Manuel
Manuel

Reputation: 11489

How can I clear the Jest cache?

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?

Upvotes: 293

Views: 311294

Answers (5)

saumyajain125
saumyajain125

Reputation: 168

If using nx monorepo run following command to clear the cache

nx test <packageName> --clearCache

Upvotes: 9

pablorsk
pablorsk

Reputation: 4296

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.

Upvotes: 70

Paulo Coghi
Paulo Coghi

Reputation: 14969

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

Upvotes: 305

Ryan H.
Ryan H.

Reputation: 7854

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.

Upvotes: 286

Edy Ionescu
Edy Ionescu

Reputation: 1773

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.

Upvotes: 152

Related Questions