To clear Jest's cache, you can do either of the following:
#Using the Jest --clearCache
CLI Option
To clear Jest's cache, starting from Jest v22+, you can specify the --clearcache
CLI option, like so:
# Jest 22.0.0+ jest --clearCache
If Jest is not installed as a global dependency, then you can do the following:
# Jest 22.0.0+ ./node_modules/.bin/jest --clearCache
If you're using yarn, then you can run the following command:
# Jest 22.0.0+ yarn jest --clearCache
Similarly, if you're using npx
, you can use the following command:
# Jest 22.0.0+ npx jest --clearCache
This will delete the Jest cache directory and exit without running tests.
For convenience, you can add it to your package.json
file, for example, in the following way:
{
...
"scripts:" {
"jest:clr": "jest --clearCache"
}
...
}
Then you can simply run the npm run jest:clr
(or similar) command.
#Locating and Removing the Cache Directory
You can locate the cache directory using the following command, and then remove that folder:
jest --showConfig | grep cacheDir
If Jest is not installed as a global dependency, then you can do the following:
./node_modules/.bin/jest --showConfig | grep cacheDir
If you're using yarn, then you can run the following command:
yarn jest --showConfig | grep cacheDir
Similarly, if you're using npx
, you can use the following command:
npx jest --showConfig | grep cacheDir
Once you have the directory location, you can use the remove command in your shell console. For example, in most shell consoles, the command would be:
rm -rf /path/to/cacheDir
You can use this method to clear cache for Jest below v22.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.