Troubleshooting

Uh oh, something went wrong? Use this guide to resolve issues with Jest.

Tests are Failing and You Don’t Know Why

Try using the debugging support built into Node. Place a debugger; statement in any of your tests, and then, in your project’s directory, run:

  1. node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here]
  2. or on Windows
  3. node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]

This will run Jest in a Node process that an external debugger can connect to. Note that the process will pause until the debugger has connected to it.

To debug in Google Chrome (or any Chromium-based browser), open your browser and go to chrome://inspect and click on “Open Dedicated DevTools for Node”, which will give you a list of available node instances you can connect to. Click on the address displayed in the terminal (usually something like localhost:9229) after running the above command, and you will be able to debug Jest using Chrome’s DevTools.

The Chrome Developer Tools will be displayed, and a breakpoint will be set at the first line of the Jest CLI script (this is done to give you time to open the developer tools and to prevent Jest from executing before you have time to do so). Click the button that looks like a “play” button in the upper right hand side of the screen to continue execution. When Jest executes the test that contains the debugger statement, execution will pause and you can examine the current scope and call stack.

Troubleshooting - 图1note

The --runInBand cli option makes sure Jest runs the test in the same process rather than spawning processes for individual tests. Normally Jest parallelizes test runs across processes but it is hard to debug many processes at the same time.

Debugging in VS Code

There are multiple ways to debug Jest tests with Visual Studio Code’s built-in debugger.

To attach the built-in debugger, run your tests as aforementioned:

  1. node --inspect-brk node_modules/.bin/jest --runInBand [any other arguments here]
  2. or on Windows
  3. node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand [any other arguments here]

Then attach VS Code’s debugger using the following launch.json config:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "type": "node",
  6. "request": "attach",
  7. "name": "Attach",
  8. "port": 9229
  9. }
  10. ]
  11. }

To automatically launch and attach to a process running your tests, use the following configuration:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Debug Jest Tests",
  6. "type": "node",
  7. "request": "launch",
  8. "runtimeArgs": [
  9. "--inspect-brk",
  10. "${workspaceRoot}/node_modules/.bin/jest",
  11. "--runInBand"
  12. ],
  13. "console": "integratedTerminal",
  14. "internalConsoleOptions": "neverOpen"
  15. }
  16. ]
  17. }

or the following for Windows:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Debug Jest Tests",
  6. "type": "node",
  7. "request": "launch",
  8. "runtimeArgs": [
  9. "--inspect-brk",
  10. "${workspaceRoot}/node_modules/jest/bin/jest.js",
  11. "--runInBand"
  12. ],
  13. "console": "integratedTerminal",
  14. "internalConsoleOptions": "neverOpen"
  15. }
  16. ]
  17. }

If you are using Facebook’s create-react-app, you can debug your Jest tests with the following configuration:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "Debug CRA Tests",
  6. "type": "node",
  7. "request": "launch",
  8. "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/react-scripts",
  9. "args": [
  10. "test",
  11. "--runInBand",
  12. "--no-cache",
  13. "--env=jsdom",
  14. "--watchAll=false"
  15. ],
  16. "cwd": "${workspaceRoot}",
  17. "console": "integratedTerminal",
  18. "internalConsoleOptions": "neverOpen"
  19. }
  20. ]
  21. }

More information on Node debugging can be found here.

Debugging in WebStorm

WebStorm has built-in support for Jest. Read Testing With Jest in WebStorm to learn more.

Caching Issues

The transform script was changed or Babel was updated and the changes aren’t being recognized by Jest?

Retry with --no-cache. Jest caches transformed module files to speed up test execution. If you are using your own custom transformer, consider adding a getCacheKey function to it: getCacheKey in Relay.

Unresolved Promises

If a promise doesn’t resolve at all, this error might be thrown:

  1. - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.`

Most commonly this is being caused by conflicting Promise implementations. Consider replacing the global promise implementation with your own, for example globalThis.Promise = jest.requireActual('promise'); and/or consolidate the used Promise libraries to a single one.

If your test is long running, you may want to consider to increase the timeout by calling jest.setTimeout

  1. jest.setTimeout(10000); // 10 second timeout

Watchman Issues

Try running Jest with --no-watchman or set the watchman configuration option to false.

Also see watchman troubleshooting.

Tests are Extremely Slow on Docker and/or Continuous Integration (CI) server.

While Jest is most of the time extremely fast on modern multi-core computers with fast SSDs, it may be slow on certain setups as our users have discovered.

Based on the findings, one way to mitigate this issue and improve the speed by up to 50% is to run tests sequentially.

In order to do this you can run tests in the same thread using --runInBand:

  • npm
  • Yarn
  • pnpm
  1. # Using Jest CLI
  2. jest --runInBand
  3. # Using your package manager's `test` script (e.g. with create-react-app)
  4. npm test -- --runInBand
  1. # Using Jest CLI
  2. jest --runInBand
  3. # Using your package manager's `test` script (e.g. with create-react-app)
  4. yarn test --runInBand
  1. # Using Jest CLI
  2. jest --runInBand
  3. # Using your package manager's `test` script (e.g. with create-react-app)
  4. pnpm test -- --runInBand

Another alternative to expediting test execution time on Continuous Integration Servers such as Travis-CI is to set the max worker pool to ~4. Specifically on Travis-CI, this can reduce test execution time in half. Note: The Travis CI free plan available for open source projects only includes 2 CPU cores.

  • npm
  • Yarn
  • pnpm
  1. # Using Jest CLI
  2. jest --maxWorkers=4
  3. # Using your package manager's `test` script (e.g. with create-react-app)
  4. npm test -- --maxWorkers=4
  1. # Using Jest CLI
  2. jest --maxWorkers=4
  3. # Using your package manager's `test` script (e.g. with create-react-app)
  4. yarn test --maxWorkers=4
  1. # Using Jest CLI
  2. jest --maxWorkers=4
  3. # Using your package manager's `test` script (e.g. with create-react-app)
  4. pnpm test -- --maxWorkers=4

If you use GitHub Actions, you can use github-actions-cpu-cores to detect number of CPUs, and pass that to Jest.

  1. - name: Get number of CPU cores
  2. id: cpu-cores
  3. uses: SimenB/github-actions-cpu-cores@v1
  4. - name: run tests
  5. run: yarn jest --max-workers ${{ steps.cpu-cores.outputs.count }}

Another thing you can do is use the shard flag to parallelize the test run across multiple machines.

coveragePathIgnorePatterns seems to not have any effect.

Make sure you are not using the babel-plugin-istanbul plugin. Jest wraps Istanbul, and therefore also tells Istanbul what files to instrument with coverage collection. When using babel-plugin-istanbul, every file that is processed by Babel will have coverage collection code, hence it is not being ignored by coveragePathIgnorePatterns.

Defining Tests

Tests must be defined synchronously for Jest to be able to collect your tests.

As an example to show why this is the case, imagine we wrote a test like so:

  1. // Don't do this it will not work
  2. setTimeout(() => {
  3. it('passes', () => expect(1).toBe(1));
  4. }, 0);

When Jest runs your test to collect the tests it will not find any because we have set the definition to happen asynchronously on the next tick of the event loop. This means when you are using test.each you cannot set the table asynchronously within a beforeEach / beforeAll.

Still unresolved?

See Help.