Skip to content

[Bug]: Path coverage thresholds include unexpected files with similar paths #12703

@j3tan

Description

@j3tan

Version

27.5.1

Steps to reproduce

  1. Clone my repro repo: https://github.com/j3tan/jest-coverage-path-issue
  2. npm install
  3. npm test
  4. Observe failure

Expected behavior

I expect to see src/Foo/ to pass with 100% coverage when using this configuration:

coverageThreshold: {
    'src/Foo/': {
        statements: 100,
        branches: 100,
        functions: 100,
        lines: 100,
    }
}

The ending / in the path group should be respected

Actual behavior

I see src/Foo/ failed with 50% coverage because src/Foobar/ is also aggregated.

--------------|---------|----------|---------|---------|-------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------|---------|----------|---------|---------|-------------------
All files     |      50 |      100 |      50 |      50 |                   
 Foo          |     100 |      100 |     100 |     100 |                   
  add.js      |     100 |      100 |     100 |     100 |                   
 Foobar       |       0 |      100 |       0 |       0 |                   
  subtract.js |       0 |      100 |       0 |       0 | 2                 
--------------|---------|----------|---------|---------|-------------------
Jest: "src/Foo/" coverage threshold for statements (100%) not met: 50%
Jest: "src/Foo/" coverage threshold for lines (100%) not met: 50%
Jest: "src/Foo/" coverage threshold for functions (100%) not met: 50%

Additional context

The ending / is ignored due to these lines:

https://github.com/facebook/jest/blob/fdc74af37235354e077edeeee8aa2d1a4a863032/packages/jest-reporters/src/CoverageReporter.ts#L281-L289

The path.resolve removes the ending slash.

Using glob matcher is not an option here since that checks per-file coverage thresholds instead of the aggregate path style.

Environment

System:
    OS: macOS 12.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.1/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  npmPackages:
    jest: ^27.5.1 => 27.5.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions