How can I clear the Jest cache?
Asked Answered
S

5

280

Jest is picking up an old version of a package and thus my tests fail unless I use --no-cache. I can even delete the package folder from folder node_modules and Jest is happy to run the tests (almost all are passing).

So how do I clear the Jest cache?

Skinnydip answered 2/7, 2017 at 2:38 Comment(0)
C
152

You can find the cache location by running jest --showConfig. Look for the cacheDirectory key. Its value is the name of the folder you'll need to remove.

Connolly answered 13/7, 2017 at 15:9 Comment(2)
is there any reason the cache location should be anywhere outside the project the tests run in?Canaday
@Canaday why do you expect any reasons from facebook team?Speight
K
287

Just run:

jest --clearCache

If you have installed Jest as a dependency in your Node.js project and the jest command doesn't work, just create a new script inside your package.json file.

{
    ...
    "scripts:" {
        "clear_jest": "jest --clearCache"
    }
    ...
}

And then, run in your terminal:

npm run clear_jest

With modern NPM, you could also run (credits to johny):

npx jest --clearCache
Knish answered 8/5, 2018 at 18:6 Comment(3)
With modern NPM, you could also run npx jest --clearCacheSelfconscious
If you have installed Jest as a dependency, then you likely have added a script to run it. Assuming that script is named 'test': npm run test -- --clearCache willl pass the clearCache flag along.Constitutional
This solved my issue - github.com/storybookjs/storybook/issues/7152. I didnt have direct Jest. Used npx jest --clearCacheGainor
H
284

As of Jest 22.0.0+, you can use the --clearCache option:

Deletes the Jest cache directory and then exits without running tests. Will delete cacheDirectory if the option is passed, or Jest's default cache directory.

Hemispheroid answered 12/10, 2017 at 21:3 Comment(0)
C
152

You can find the cache location by running jest --showConfig. Look for the cacheDirectory key. Its value is the name of the folder you'll need to remove.

Connolly answered 13/7, 2017 at 15:9 Comment(2)
is there any reason the cache location should be anywhere outside the project the tests run in?Canaday
@Canaday why do you expect any reasons from facebook team?Speight
P
67

First, you need to know the Jest version:

yarn jest --version

Jest >= 22.0.0

yarn jest --clearCache

Jest < 22.0.0

yarn jest --showConfig | grep cacheDir

Returns (you need to remove that folder)

      "cacheDirectory": "/tmp/jest_rs",

Then, you remove it

rm -rf /tmp/jest_rs

If you don’t use Yarn, do instructions with npx jest.

Praedial answered 14/9, 2019 at 17:52 Comment(0)
C
8

If using nx monorepo run following command to clear the cache

nx test <packageName> --clearCache
Cystolith answered 1/12, 2022 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.