How to clear or disable the Cache in Jest [4 Ways]

avatar
Borislav Hadzhiev

Last updated: Mar 7, 2024
4 min

banner

# Table of Contents

  1. How to clear the Cache in Jest
  2. Clear the Jest Cache by adding a script to your package.json file
  3. Using the jest.resetModules() helper method
  4. Disabling the Cache in Jest

# How to clear the Cache in Jest

Use 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.

shell
npx jest --clearCache

npx jest clear cache

If you have jest installed globally, you can also run the command directly.

shell
jest --clearCache

If you get the error "jest: command not found", use npx jest --clearCache or install jest globally.

shell
npm install -g jest jest --clearCache

install jest globally and clear cache

The --clearCache option:

  1. Deletes the Jest cache directory.
  2. Exits without running your tests.

The default cache directory can be found by using the --showConfig option.

shell
npx jest --showConfig

get cache directory location

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.

shell
npx jest --showConfig | grep cacheDirectory

get jest cache directory with grep

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.

shell
# for macOS and Linux rm -rf /tmp/jest_rs

delete cache directory macos linux

Make sure to specify the correct path to your cache directory.

If you are on Windows, you would use the rd command in CMD.

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.

# Clear the Jest Cache by adding a script to your package.json file

Alternatively, 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.

  1. Open your terminal in your project's root directory (where your package.json file is) and make sure you have jest installed as a development dependency.
shell
# with NPM npm install jest --save-dev # or with YARN yarn add jest --dev

install jest as dev dependency

  1. Add the following script to the scripts object in your package.json file.
package.json
{ "scripts": { "clear-jest-cache": "jest --clearCache" }, "dependencies": {}, "devDependencies": {} }

add clear jest cache script

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

shell
npm run clear-jest-cache

npm run clear jest cache

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

shell
yarn clear-jest-cache

yarn clear jest cache

# Using the jest.resetModules() helper method

Jest 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.

index.test.js
beforeEach(() => { jest.resetModules(); });

The helper method is usually used to isolate modules where the local state might conflict between tests.

# Disabling the Cache in Jest

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.

shell
npx jest --no-cache

disable jest cache

If you have Jest globally installed, you can run the command directly.

shell
jest --no-cache

If you get the error:

  • 'jest: command not found'
  • or 'jest is not recognized as an internal or external command'

Use npx jest instead of jest.

You can also add the --no-cache flag to your test script in package.json.

package.json
{ "scripts": { "test": "jest --no-cache" }, "dependencies": {}, "devDependencies": {} }

disable jest cache in package json

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

shell
npm run test

run tests with caching disabled npm

If you use yarn, use the following command instead.

shell
yarn test

run tests with caching disabled yarn

You can read more about the --no-cache flag in this section of the Jest docs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.