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?


Solution 1:

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.

Solution 2:

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