πŸ’‘ This page contain affiliate links. By making a purchase through them, we may earn a commission at no extra cost to you.
Two Easy Ways to Clear the Cache in Jest

Two Easy Ways to Clear the Cache in Jest

Ferenc Almasi β€’ 2022 July 17 β€’ Read time 2 min read
  • twitter
  • facebook

Depending on your Jest version, you have two different options to clear the cache for Jest. Starting with newer versions, Jest above v22 has its own built-in command for removing the cache.

jest --clearCache

Just pass the --clearCache option to your Jest CLI to get the cache removed for Jest. You can also create a new NPM script inside your project by adding the following line to your package.json file:

Copied to clipboard!
{
    "scripts": {
        "clean": "jest --clearCache"
    }
}

Then you can run npm run clean in your terminal to execute the command. For newer versions of NPM where NPX is also pre-bundled (above v5.2), you can also do npx jest --clearCache to run the necessary binaries.


If you are using Jest below version 22, you should immediately look into upgrading your packages. But if you don't have time for this, you can remove the cache directory for these versions using the following two commands inside your terminal:

  - jest --showConfig | grep cacheDir
- rm -rf path/to/cache

The first command will use jest --showConfig to grab the configuration object of Jest and grep cacheDir will grab the cacheDirectory property from that object. This will output the exact location of your Jest cache directory to the console. Something similar to:

Copied to clipboard!
"cacheDirectory": "C:\\Users\\user\\AppData\\Local\\Temp\\jest"
The full location of the cache directory for Jest

Then you can use the rm -rf command passing the retrieved location to remove the folder, clearing the cache.

  • twitter
  • facebook
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.