265

Jest is picking up an old version of a package and thus my tests fail unless I use --no-cache. I can even delete the package folder from folder node_modules and Jest is happy to run the tests (almost all are passing).

So how do I clear the Jest cache?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manuel
  • 10,869
  • 14
  • 55
  • 86

5 Answers5

274

As of Jest 22.0.0+, you can use the --clearCache option:

Deletes the Jest cache directory and then exits without running tests. Will delete cacheDirectory if the option is passed, or Jest's default cache directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan H.
  • 7,374
  • 4
  • 39
  • 46
263

Just run:

jest --clearCache

If you have installed Jest as a dependency in your Node.js project and the jest command doesn't work, just create a new script inside your package.json file.

{
    ...
    "scripts:" {
        "clear_jest": "jest --clearCache"
    }
    ...
}

And then, run in your terminal:

npm run clear_jest

With modern NPM, you could also run (credits to johny):

npx jest --clearCache
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paulo Coghi
  • 13,724
  • 14
  • 68
  • 90
  • 52
    With modern NPM, you could also run `npx jest --clearCache` – Johny Sep 25 '18 at 10:52
  • 4
    If you have installed Jest as a dependency, then you likely have added a script to run it. Assuming that script is named 'test': `npm run test -- --clearCache` willl pass the clearCache flag along. – Darc Aug 27 '21 at 23:54
  • 1
    This solved my issue - https://github.com/storybookjs/storybook/issues/7152. I didnt have direct Jest. Used `npx jest --clearCache` – Mehul Parmar Feb 09 '23 at 12:38
152

You can find the cache location by running jest --showConfig. Look for the cacheDirectory key. Its value is the name of the folder you'll need to remove.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Edy Ionescu
  • 1,665
  • 1
  • 12
  • 7
  • is there any reason the cache location should be anywhere outside the project the tests run in? – rob Sep 29 '20 at 15:40
61

First, you need to know the Jest version:

yarn jest --version

Jest >= 22.0.0

yarn jest --clearCache

Jest < 22.0.0

yarn jest --showConfig | grep cacheDir

Returns (you need to remove that folder)

      "cacheDirectory": "/tmp/jest_rs",

Then, you remove it

rm -rf /tmp/jest_rs

If you don’t use Yarn, do instructions with npx jest.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pablorsk
  • 3,861
  • 1
  • 32
  • 37
5

If using nx monorepo run following command to clear the cache

nx test <packageName> --clearCache
saumyajain125
  • 93
  • 1
  • 4