Two Easy Ways to Clear the Cache in Jest
Depending on your Jest version, you have two different options to clear the cache for Jest. Starting with newer versions, Jest above v22 has its own built-in command for removing the cache.
jest --clearCache
Just pass the --clearCache
option to your Jest CLI to get the cache removed for Jest. You can also create a new NPM script inside your project by adding the following line to your package.json
file:
{
"scripts": {
"clean": "jest --clearCache"
}
}
Then you can run npm run clean
in your terminal to execute the command. For newer versions of NPM where NPX is also pre-bundled (above v5.2), you can also do npx jest --clearCache
to run the necessary binaries.
If you are using Jest below version 22, you should immediately look into upgrading your packages. But if you don't have time for this, you can remove the cache directory for these versions using the following two commands inside your terminal:
- jest --showConfig | grep cacheDir
- rm -rf path/to/cache
The first command will use jest --showConfig
to grab the configuration object of Jest and grep cacheDir
will grab the cacheDirectory
property from that object. This will output the exact location of your Jest cache directory to the console. Something similar to:
"cacheDirectory": "C:\\Users\\user\\AppData\\Local\\Temp\\jest"
Then you can use the rm -rf
command passing the retrieved location to remove the folder, clearing the cache.
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: