jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Coverage report invalid coverage for files when test file changes

Open sandinosaso opened this issue 2 years ago • 8 comments

Version

29.0.1

Steps to reproduce

  1. Clone my repo at: https://github.com/sandinosaso/jest-coverage-bug-reproduction.git
  2. Run yarn install
  3. Change something (add an space at the end) to the file src/b.test.js
  4. Run yarn test --coverage --changedSince=origin/master --shard=1/1

Expected behavior

I expect either:

  1. Because the require of a.js in b.test.js determined that coverage should be show for that a.js file I would expect all the tests of a.js are run (i.e the file a.test.js is run because has tests that belong to a.js and that we are going to coverage report
  2. Do not show any coverage report for a.js file because this file was not changed

Actual behavior

In the console of the left the coverage report shows 100% for the only changed file (b.js) - This is the expected behavior

In the console of the right side you see running the same command after adding the require('./a.js') see that now the coverage is also shown for the a.js file and it is not 100% percent as expected:

Screenshot 2022-09-02 at 16 28 35

Additional context

Somehow may be related, there is a difference when we run yarn test FILEPATH in that case we see also that (not matter if there is a require o not in the test file testing) then the coverage it not shown complete for the file as you can see above:

Screenshot 2022-09-02 at 15 45 38

Environment

System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
  Binaries:
    Node: 14.15.5 - ~/.nvm/versions/node/v14.15.5/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v14.15.5/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v14.15.5/bin/npm
  npmPackages:
    jest: ^29.0.1 => 29.0.1

sandinosaso avatar Sep 02 '22 14:09 sandinosaso

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Oct 02 '22 14:10 github-actions[bot]

My team is also effected by this - @sandinosaso, did you come up with any workarounds? We're having a tough time enforcing coverage thresholds because when test files change we end up with a lot of files (imported from tests) not getting covered because their corresponding tests are not run.

cdriscol avatar Oct 06 '22 19:10 cdriscol

@cdriscol We handled this scenario by marking imported files as changed. To do this we created a method which merges imported files and changed files. This set of files is then passed to jest method this.findRelatedTests(mergedFiles, collectCoverage); which collects coverage from imported files as well.

async findTestRelatedToChangedAndImportedFiles(changedFilesInfo, collectCoverage) {
    if (!hasSCM(changedFilesInfo)) {
      return {
        noSCM: true,
        tests: []
      };
    }

    const {changedFiles} = changedFilesInfo;

    const importedFiles = await this.findRelatedSourcesFromTestsInChangedFiles(changedFilesInfo); //imported files which are marked for coverage
    const mergedFiles = new Set([...changedFiles, ...importedFiles]);

    return this.findRelatedTests(mergedFiles, collectCoverage);
  }

call this method in -

async _getTestPaths(globalConfig, changedFiles) {
    if (globalConfig.onlyChanged) {
      if (!changedFiles) {
        throw new Error('Changed files must be set when running with -o.');
      }
      return this.findTestRelatedToChangedAndImportedFiles(
        changedFiles,
        globalConfig.collectCoverage
      );
    }
    ...
    ...
    ...
}

Only drawback is if the code is tightly coupled this will run lot of test cases as it calls resolveInverseModuleMap() internally which jest use to resolve inverse dependency (inverse transitive dependency).

You can use patch-package to apply changes to your repo.

snehalDH avatar Nov 01 '22 15:11 snehalDH

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Dec 01 '22 15:12 github-actions[bot]

@snehalDH Hello , can you tell me which file are the _getTestPaths function in ? I 'm not familiar with jest source code. Thanks a lot.

sjnho avatar Dec 16 '22 06:12 sjnho

any solution for this point?

xabierlameiro avatar Dec 17 '22 22:12 xabierlameiro

@sjnho , you can find it _getTestPaths here .

snehalDH avatar Dec 18 '22 19:12 snehalDH

Someone received the expected result? I tried to use a solution by @snehalDH But unfortunately doesn't work for me

vshevchenko12 avatar Jan 12 '23 21:01 vshevchenko12

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 11 '23 21:02 github-actions[bot]

shevchenko12, can you elaborate what exactly didn't work for you? I can try to help.

snehalDH avatar Feb 13 '23 08:02 snehalDH

@snehalDH I tried again and it works now. Thank you very much for your solution!

vshevchenko12 avatar Mar 07 '23 04:03 vshevchenko12

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Apr 06 '23 04:04 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar May 06 '23 04:05 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Jun 06 '23 00:06 github-actions[bot]