Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving src files doesn't bust the Jest cache #3705

Closed
adityavohra7 opened this issue May 31, 2017 · 18 comments
Closed

Moving src files doesn't bust the Jest cache #3705

adityavohra7 opened this issue May 31, 2017 · 18 comments

Comments

@adityavohra7
Copy link

adityavohra7 commented May 31, 2017

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
@ulrik59
Copy link

ulrik59 commented Jun 14, 2017

I have the same problem. I have to run all my tests with --no-cache to get the real coverage...

@nerfologist
Copy link

Also experiencing this issue.

Also, as a workaround, it would be nice to have more info on how to flush the cache manually in case this happens (--no-cache ignores it, but it doesn't reset it).

@ljharb
Copy link
Contributor

ljharb commented Jun 29, 2017

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

@nerfologist
Copy link

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

@thymikee
Copy link
Collaborator

@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

@ljharb
Copy link
Contributor

ljharb commented Jun 30, 2017

Thanks, that helps. jest really needs a clear cache command :-/

@michaelgmcd
Copy link

Having this issue as well. I have two files with paths app/controllers/index.js and app/stages/index.js and they both have unit tests. For some reason, the coverage report says that app/stages/index.js is never covered. Even if I only run the app/stages/index.js test, app/controllers/index.js has 100% coverage and the stages file has 0%

Removing the cache and running jest does not work, but running jest --no-cache works as expected and says that both files are properly covered.

@nerfologist
Copy link

nerfologist commented Jul 3, 2017

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')"
  }
}

vjeux added a commit that referenced this issue Aug 9, 2017
This explanation by @thymikee worked like a charm! #3705 (comment)
cpojer pushed a commit that referenced this issue Aug 9, 2017
This explanation by @thymikee worked like a charm! #3705 (comment)
tushardhole pushed a commit to tushardhole/jest that referenced this issue Aug 21, 2017
@stipsan
Copy link
Contributor

stipsan commented Sep 6, 2017

Hey guys,

Hopefully you won't need a clear cache command anymore after this is released: #4432

@cpojer
Copy link
Member

cpojer commented Sep 6, 2017

Ah nice! Closing this task.

@cpojer cpojer closed this as completed Sep 6, 2017
@tabrindle
Copy link
Contributor

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

#4430

@AndrewRayCode
Copy link

AndrewRayCode commented Dec 17, 2017

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!

@Berkmann18
Copy link

Berkmann18 commented Apr 13, 2019

jest --clearCache and jest --no-cache doesn't seem to help in making sure that jest stops looking for an old filename (both the test suite and tested file were renamed from labelClass to findCategory).

Cannot find module '../findCategory' from 'labelClass.js'
 > 1 | import {findBestCategory} from '../findCategory'

And it worked well before renaming both files.
Also, whenever I try to re-run failed tests (on yarn) it says "No failed test found"...
EDIT: Renaming he ../findCategory to ../labelClass _then running jest --clearCache then yarn test (while keeping the new module name, i.e findCategory.js) makes it work.

@moodseller
Copy link

moodseller commented Aug 14, 2019

Having a similar issue, running jest as it is works fine, however if I want to add a --watch to it, it keeps giving me an error of Could not locate module even though it locates without the --watch and my application runs without any issues.

Could not locate module @lib/token/token mapped as:
    /Users/privateUser/Documents/api/lib/token/token.
Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/@lib\/(.*)/": "/Users/privateUser/Documents/api/lib/$1"
      },
      "resolver": null
    }
moduleNameMapper: {
		'@lib/(.*)': '<rootDir>/lib/$1'
	},

Nowhere in my project do I have such an import as it was an old one and it was modified to @lib/token.

@SimenB
Copy link
Member

SimenB commented Sep 4, 2019

Please open new issues, with reproductions, rather than commenting on old closed ones

@SergeyKatugin
Copy link

Having same problem. My solution was remove coverage folder from project. And restart

@crisu83
Copy link

crisu83 commented May 27, 2020

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')"
  }
}

Alternatively you can also use grep if you don't have jq available in your environment.

// package.json
{
  "scripts": {
    "test:clearCache": "rimraf $(./node_modules/.bin/jest --showConfig | grep -o '\"cacheDirectory\": \"[^\"]*' | grep -o '[^\"]*$')"
  }
}

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests