Last updated: Mar 7, 2024
Reading time·4 min

package.json filejest.resetModules() helper methodUse the --clearCache to clear the cache in Jest, e.g.
npx jest --clearCache.
When the --clearCache option is set, the command deletes the Jest cache
directory and exits without running your tests.
npx jest --clearCache

If you have jest installed globally, you can also run the command directly.
jest --clearCache
If you get the error "jest: command not found", use npx jest --clearCache or
install jest globally.
npm install -g jest jest --clearCache

The --clearCache option:
The default cache directory can be found by using the --showConfig option.
npx jest --showConfig

The output from the command is a quite large object, however, you should be able
to see the cacheDirectory key toward the top.
The screenshot shows that my Jest cache directory is /tmp/jest_rs.
This will likely be different in your case.
If you have grep installed, you can also pipe the output of the command to
grep to only get the value of the cacheDirectory key.
npx jest --showConfig | grep cacheDirectory

You can also clear the Jest cache by deleting your cache directory.
For example, if you are on macOS or Linux, you would issue the following command.
# for macOS and Linux rm -rf /tmp/jest_rs

Make sure to specify the correct path to your cache directory.
If you are on Windows, you would use the rd command in CMD.
rd /s /q "path\to\cache\directory"
Make sure to specify the path to your cache directory as shown in the
npx jest --showConfig command.
package.json fileAlternatively, you can add a clear-jest-cache script to your package.json
file to not have to remember the command you have to issue to clear your Jest
cache.
package.json file is) and make sure you have
jest installed as a development
dependency.# with NPM npm install jest --save-dev # or with YARN yarn add jest --dev

scripts object in your package.json file.{ "scripts": { "clear-jest-cache": "jest --clearCache" }, "dependencies": {}, "devDependencies": {} }

Now you can issue the npm run clear-jest-cache command to clear your Jest
cache.
npm run clear-jest-cache

If you use yarn, use the yarn clear-jest-cache command instead.
yarn clear-jest-cache

jest.resetModules() helper methodJest also has a jest.resetModules helper method that enables you to reset the cache of all required modules.
For example, you could run the method in a beforeEach() hook to reset the
cache before each test.
beforeEach(() => { jest.resetModules(); });
The helper method is usually used to isolate modules where the local state might conflict between tests.
If you need to disable the cache, set the --no-cache parameter when issuing
the jest command.
Note that the cache should only be disabled if you constantly run into caching issuing when using Jest.
If you disable the cache, Jest will be about two times slower on average.
npx jest --no-cache

If you have Jest globally installed, you can run the command directly.
jest --no-cache
If you get the error:
Use npx jest instead of jest.
You can also add the --no-cache flag to your test script in package.json.
{ "scripts": { "test": "jest --no-cache" }, "dependencies": {}, "devDependencies": {} }

Now you can issue the npm run test command to run your test with caching
disabled.
npm run test

If you use yarn, use the following command instead.
yarn test

You can read more about the --no-cache flag in
this section of the Jest docs.
You can learn more about the related topics by checking out the following tutorials:
process function