jest: Moving src files doesn't bust the Jest cache

Do you want to request a feature or report a bug?

Bug report.

What is the current behavior?

Moving a source file doesn’t seem to bust the Jest cache. This causes the default coverage reporter to report missing coverage on the moved file in the new location, and report full coverage on the moved file in the old location.

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

Here is my src dir structure:

$ tree src/
src/
├── components
│   └── Example
│       └── README.md
├── foo
│   └── index.js
└── scss
    └── index.scss

4 directories, 3 files

Running jest before moving index.js:

$ ./node_modules/.bin/jest --config jest.config.js
 PASS  tests/components/example.test.js
  ExampleComponent
    ✓ renders all the right stuff (15ms)
    ✓ passes a snapshot test (8ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   1 passed, 1 total
Time:        1.598s, estimated 2s
Ran all test suites.
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
 index.js |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

Moving index.js:

$ mv src/foo/index.js src/components/Example/
$ tree src/
src/
├── components
│   └── Example
│       ├── index.js
│       └── README.md
└── scss
    └── index.scss

3 directories, 3 files

Rerunning Jest after moving index.js (and updating the import statement in the test):

 $ ./node_modules/.bin/jest --config jest.config.js
 PASS  tests/components/example.test.js
  ExampleComponent
    ✓ renders all the right stuff (15ms)
    ✓ passes a snapshot test (8ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   1 passed, 1 total
Time:        1.628s, estimated 2s
Ran all test suites.
--------------------|----------|----------|----------|----------|----------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
--------------------|----------|----------|----------|----------|----------------|
All files           |     4.76 |        0 |       20 |       20 |                |
 components/Example |        0 |        0 |        0 |        0 |                |
  index.js          |        0 |        0 |        0 |        0 |        1,3,5,6 |
 foo                |      100 |      100 |      100 |      100 |                |
  index.js          |      100 |      100 |      100 |      100 |                |
--------------------|----------|----------|----------|----------|----------------|
Jest: Coverage for statements (4.76%) does not meet global threshold (100%)
Jest: Coverage for branches (0%) does not meet global threshold (100%)
Jest: Coverage for lines (20%) does not meet global threshold (100%)
Jest: Coverage for functions (20%) does not meet global threshold (100%)

What is the expected behavior?

Jest should detect that I’ve moved the file. Running Jest with --no-cache “fixes” this (coverage is as expected). Rerunning without --no-cache causes the issue again (cache isn’t cleared?). Editing the moved file however fixes the problem.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

Jest config:

module.exports = {
  collectCoverage: true,

  collectCoverageFrom: [
    'src/**/*.js'
  ],

  coverageThreshold: {
    global: {
      branches: 100,
      functions: 100,
      lines: 100,
      statements: 100
    }
  },

  snapshotSerializers: [
    'enzyme-to-json/serializer'
  ]
};

Version details:

$ ./node_modules/.bin/jest --version
v20.0.4

$ node --version
v5.11.0

$ npm --version
3.8.6

$ uname -a
Linux dev36-devc 4.2.0-42-generic #49~14.04.1-Ubuntu SMP Wed Jun 29 20:22:11 UTC 2016 x86_64 GNU/Linux

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 28
  • Comments: 18 (6 by maintainers)

Commits related to this issue

Most upvoted comments

In case cache clearing is needed in the future, as of 21.3.0, there is a new cli option --clearCache

https://github.com/facebook/jest/pull/4430

@nerfologist you can jest --showConfig, search for “cacheDirectory” key and just remove that folder if you want to purge the cache completely for some reason

Not sure if this is the same ticket, but I had jest watch report that a test file was completely passing,even after editing that specific test file, and even after re-running all tests with a, and even when running a single test not in watch mode, even though it was actually failing. Finding and removing the cache directory made me see actual test results.

I’ve been moving a few files so not sure if related, but sure is scary to know that the tests I’m running have no guarantee to be accurate!

Hi @ljharb , I spent a lot of time trying to figure out where the cache files were created, in my configuration (Mac Os X Sierra, react-scripts 1.0.7 for create-react-app 1.3.1) I found the cache files in the /private/var/folders/zj/112vf5bj0js_hx6l9ntz3w780000gn/T/jest_dxdirectory.

Hope this info can be useful to you if you want to clear them out:

✔  09:31 {ruby-2.4.0} {node v8.1.2} [/private/var/folders/zj/112vf5bj0js_hx6l9ntz3w780000gn/T/jest_dx]
✎  $ tree .
.
├── haste-map-6baae784957a7b05d31a1cb3f11cd62f-db0bbdff794026d6fe1c81c15eb00f76
├── haste-map-972935e3c872cc2710b886add2b47c99-9d19e3ac6c85c10f406f2d517029338b
└── haste-map-d9a696895ec6b2ddc29d4b43f785986b-ef8320c62d38fbedac43da58d7b811b3

0 directories, 3 files

In order to locate them on your machine, I’d issue a find /private/var/folders -type f -name haste-map-\* 2>/dev/null

I’m seeing this same problem, and I can’t figure out how to clear the cache - only --no-cache works.

Workaround npm script to zap jest cache (requires rimraf and jq, not windows-portable), based on @thymikee’s suggestion :

// package.json
{
  "scripts": {
    "test:clearCache": "rimraf $(./node_modules/jest/bin/jest.js --showConfig | jq '.config.cacheDirectory')"
  }
}

Thanks, that helps. jest really needs a clear cache command 😕