Jest cache - Disable, Find Cache Directory with examples
This tutorial explains about below things
jest cache allows you to run the test cases faster to improve the execution time.
Jest provides multiple options to run in any javascript projects
- jest command line.
How to disable cache in jest test cases
the jest command line provides two options to enable or disable the cache
--cache
: Enable the cache, It is a default option.--no-cache
: Disable the cache
jest --cache
jest by default store its in a temporary cache for performance improvement
You can also override the location in the jest.config.js
module.exports={
cacheDirectory:"/var/project/jest"
}
Run the below command, to disable the cache. and slow down the execution.
jest --no-cache
Jest Cache Directory Location
Jest configurations are defined as jest.config.js or jest.config.json.
You can find the location of the cache folder in two ways.
Using config file
Open jest.config.js in VS Code
It contains cacheDirectory attribute that contains location jest cache
cacheDirectory:"/var/project/jest"
- Using CLI —showConfig
JEST CLi provides —showConfig and lists out all configurations
jest --showConfig --config=jest.config.json
It lists all configurations. You can find cacheDirectory key
jest --showConfig --config=jest.config.json |grep cacheDirectory
Output:
"cacheDirectory": "/var/project/jest"
If you are using npm scripts, You can configure scripts in package.json
{
"scripts": {
"find": "jest --showConfig --config=jest.config.json |grep cacheDirectory"
}
}
next, You can run the npm run find
command in the command line
For some reason, if jest command is not found in your project, You can use the below options
For local dependency, use the below command
./node_modules/.bin/jest --showConfig --config=jest.config.json |grep cacheDirectory
For yarn users
yarn jest --showConfig --config=jest.config.json |grep cacheDirectory
For npx uses without installation
npx jest --showConfig --config=jest.config.json |grep cacheDirectory
How to clear Cache in Jest with an example
jest CLI provides an option —clearCache to remove cache
jest --clearCache
For local dependency, use below command
./node_modules/.bin/jest --clearCache
For yarn users
yarn jest --clearCache
For npx uses without installation
npx jest --clearCache
Another way, find the cache folder and remove using rm command
First, find the cache directory
jest --showConfig --config=jest.config.json |grep cacheDirectory
"cacheDirectory": "/var/project/jest"
It gives the location of the cache directory.
Remove the directory using the OS rm command to delete the cache
rm -rf /var/projects/jest