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
- Explain how to clear the cache This explanation by @thymikee worked like a charm! https://github.com/facebook/jest/issues/3705#issuecomment-312199902 — committed to jestjs/jest by vjeux 7 years ago
- Explain how to clear the cache (#4232) This explanation by @thymikee worked like a charm! https://github.com/facebook/jest/issues/3705#issuecomment-312199902 — committed to jestjs/jest by vjeux 7 years ago
- Explain how to clear the cache (#4232) This explanation by @thymikee worked like a charm! https://github.com/facebook/jest/issues/3705#issuecomment-312199902 — committed to tushardhole/jest by vjeux 7 years ago
- Add npm script to clear Jest cache Renaming test files caused Jest to get confused. Running this new scripts clears the Jest cache, which resolves this confusion. [1] https://remarkablemark.org/blog... — committed to wikimedia/wvui by berndsi 4 years ago
- Add npm script to clear Jest cache (#71) * Add npm script to clear Jest cache Renaming test files caused Jest to get confused. Running this new scripts clears the Jest cache, which resolves this ... — committed to wikimedia/wvui by berndsi 4 years ago
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 reasonNot 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_dx
directory.Hope this info can be useful to you if you want to clear them out:
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 :Thanks, that helps. jest really needs a clear cache command 😕