Skip to content

How to Clear the Cache in Jest in JavaScript

Learn how to clear Jest's module cache with npx jest --clearCache, add a clear-cache npm script, and temporarily disable caching for debugging problematic tests.

Jest maintains a cache of transformed module files to make running tests faster. However, this cache can sometimes become stale or corrupted after upgrading dependencies, changing configurations (like Babel or TypeScript), or when tests behave unexpectedly. Clearing the Jest cache is a common and effective troubleshooting step.

This guide will teach you the standard command-line method for clearing the cache, how to add a convenient npm script for it, and how to disable the cache for debugging purposes.

The simplest and most direct way to clear the cache is by using the --clearCache command-line flag. This command will delete the entire Jest cache directory and then exit without running any tests.

The solution is to open your terminal in your project's root directory and run the following command.

npx jest --clearCache

After the command completes, you can run your tests as usual, and Jest will create a fresh cache.

Adding a package.json Script for Convenience

To avoid having to remember the --clearCache flag, you can add a dedicated script to your package.json file.

The solution is to open your package.json file and add a clear-cache script.

{
  "scripts": {
    "test": "jest",
    "clear-cache": "jest --clearCache"
  }
}

Now, you can simply run the following command to clear the cache:

npm run clear-cache

yarn clear-cache

How to Disable the Cache for a Single Run

If you are debugging an issue that you suspect is related to caching, you can temporarily disable the cache for a single test run using the --no-cache flag.

Solution:

npx jest --no-cache

You can also add this to your test script in package.json if needed, but it's not recommended for permanent use due to the performance impact.

// In package.json (for temporary debugging only)
"scripts": {
  "test": "jest --no-cache"
}

A Note on jest.resetModules()

It's important not to confuse clearing the global Jest cache with resetting the module cache within a test suite. The jest.resetModules() helper function is used inside a test file (often in a beforeEach block) to ensure that modules are re-imported and do not share state between tests.

// This resets the module cache for each test in this file.
// It does NOT clear the global Jest file transform cache.
beforeEach(() => {
  jest.resetModules();
});

Conclusion

Clearing the Jest cache is a simple but powerful troubleshooting step for resolving unexpected test failures and configuration issues.

  • The recommended best practice is to run npx jest --clearCache from your terminal.
  • Adding a clear-cache script to your package.json is a convenient way to make this command easily accessible.
  • Use the --no-cache flag only for temporary debugging, as it has a significant performance penalty.
Navigation