How do I perform a Jest clear cache?
I’m having an issue where Jest is picking up an old package version, causing my tests to fail unless I use --no-cache
. Even if I delete the package folder from node_modules
, Jest still runs the tests (with most passing).
How to Clear the Jest Cache?
Hello Ariyas,
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.
Clearing the cache in Jest is essential when you encounter issues related to stale or corrupted cache data. To perform a Jest clear cache, follow these steps:
-
Command Line Method: Run the command
jest --clearCache
in your terminal. This command will clear Jest’s cache and ensure no code test automation run with fresh data.
-
Delete Cache Directory: Alternatively, you can manually delete the cache directory.
Clearing the cache helps resolve unexpected test failures and ensures your tests run correctly with the latest code changes. Regular cache clearing can improve test reliability and accuracy.
1 Like
Hello Ariyash,
Here is your Answer
If you have installed Jest as a dependency in your Node.js project and the jest command doesn’t work, create a new script inside your package.json file:
{
…
“scripts”: {
“clear_jest”: “jest --clearCache”
}
…
}
Then, run in your terminal:
npm run clear_jest
With modern NPM, you could also run:
npx jest --clearCache