52

Every time I run jest it never runs anything. I have let the counter go arbitrarily high. I have run jest with --no-cache

jest --debug output is as follows:

{
  "configs": [
    {
      "automock": false,
      "browser": false,
      "cache": true,
      "cacheDirectory": "/var/folders/7v/64n1tsk11zs2pbwf5bm_c9kc0000gn/T/jest_dx",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "detectLeaks": false,
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "defaultPlatform": "ios",
        "platforms": [
          "android",
          "ios",
          "native"
        ],
        "providesModuleNodeModules": [
          "react-native"
        ]
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "json",
        "jsx",
        "node"
      ],
      "moduleNameMapper": [
        [
          "^React$",
          "/Users/skilurus/github/flock-react-app/node_modules/react"
        ]
      ],
      "modulePathIgnorePatterns": [
        "/Users/skilurus/github/flock-react-app/node_modules/react-native/Libraries/react-native/"
      ],
      "name": "b29a126b130a0be47202d3bc7b00f1b4",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/skilurus/github/flock-react-app",
      "roots": [
        "/Users/skilurus/github/flock-react-app"
      ],
      "runner": "jest-runner",
      "setupFiles": [
        "/Users/skilurus/github/flock-react-app/node_modules/regenerator-runtime/runtime.js",
        "/Users/skilurus/github/flock-react-app/node_modules/react-native/jest/setup.js",
        "/Users/skilurus/github/flock-react-app/test-setup.js"
      ],
      "snapshotSerializers": [
        "/Users/skilurus/github/flock-react-app/node_modules/enzyme-to-json/serializer.js"
      ],
      "testEnvironment": "/Users/skilurus/github/flock-react-app/node_modules/jest-environment-jsdom/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "**/__tests__/**/*.js?(x)",
        "**/?(*.)(spec|test).js?(x)"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/",
        "e2e"
      ],
      "testRegex": "",
      "testRunner": "/Users/skilurus/github/flock-react-app/node_modules/jest-jasmine2/build/index.js",
      "testURL": "about:blank",
      "timers": "real",
      "transform": [
        [
          "^.+\\.js$",
          "/Users/skilurus/github/flock-react-app/node_modules/babel-jest/build/index.js"
        ],
        [
          "^[./a-zA-Z0-9$_-]+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$",
          "/Users/skilurus/github/flock-react-app/node_modules/react-native/jest/assetFileTransformer.js"
        ]
      ],
      "transformIgnorePatterns": [
        "node_modules/(?!react-native|native-base|react-navigation|react-native-fabric|tipsi-stripe)"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": false,
    "changedFilesWithAncestor": false,
    "collectCoverageFrom": [
      "src/**/*.{js,jsx}"
    ],
    "coverageDirectory": "/Users/skilurus/github/flock-react-app/__coverage__",
    "coverageReporters": [
      "json",
      "lcov",
      "text"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 70,
        "functions": 75,
        "lines": 85,
        "statements": 80
      }
    },
    "detectLeaks": false,
    "expand": false,
    "globalSetup": null,
    "globalTeardown": null,
    "listTests": false,
    "mapCoverage": false,
    "maxWorkers": 7,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "always",
    "passWithNoTests": false,
    "rootDir": "/Users/skilurus/github/flock-react-app",
    "runTestsByPath": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testResultsProcessor": null,
    "updateSnapshot": "new",
    "useStderr": false,
    "verbose": true,
    "watch": false,
    "watchman": true
  },
  "version": "22.3.0"
}

node --version: 8.9.4

npm --version: 5.6.0

yarn --version 1.3.2

Has anybody seen anything similiar? Does anybody know hot to fix this?

Izhaki
  • 23,372
  • 9
  • 69
  • 107
Abraham P
  • 15,029
  • 13
  • 58
  • 126
  • Did u get a solution for this? – Amrendra Apr 28 '20 at 11:32
  • Yes probably, but I don't know or remember what it was. The fact I didn't write up an answer to my own question here suggests that I never really figured out how to fix it, rather some random incantantation fixed a specific instance of this for me – Abraham P Apr 28 '20 at 16:54
  • I fixed it by node version change. – Amrendra Apr 29 '20 at 14:37
  • Your tests started 7 workers so it's hard to find exactly which one has an infinite loop. `--runInBand` CLI option it's good for the start to run the tests in serially. Because literally nothing happened after `163s` you need to isolate the test causing the infinite loop. – Șerban Ghiță Dec 15 '22 at 10:23

19 Answers19

28

This happens to me too but it's intermittent, very frustrating though. I have discovered a workaround which is to run using the --runInBand flag, which just runs tests in the same thread:

jest --runInBand
dst
  • 289
  • 3
  • 3
23

Run you tests like so:

jest --detectOpenHandles --forceExit 

--detectOpenHandles logs out errors preventing your tests from exiting clearly and it implies --runInBand which ensures your tests run in the same threads so no overlapping. --forceExit which terminates your tests if something is wrong instead of hanging.

Harry Adel
  • 1,238
  • 11
  • 16
18

For the record, in my case it turned out to be a caching problem (I tried many many other things before).

For jest >=22.0.0 use the --clearCache option to clear the cache.

For jest <22.0.0 use the --showConfig option, search for the cacheDirectory property and delete the mentioned directory.

Hope this helps someone.

rrhrg
  • 1,150
  • 1
  • 13
  • 15
  • 1
    What seems to work best in our CI/CD is first the answer here, `jest --clearCache` followed by one of the answers below `jest --runInBand --detectOpenHandles --forceExit` Our test time on a linux docker machine was reduced from 1 min to less than 10 seconds, with no more hangups. – KayakinKoder Nov 19 '21 at 00:39
11

On MacOS uninstalling and reinstalling watchman didn't work for me. Running watchman version hangs indefinitely.

do this to fix the watchman from hanging:

launchctl unload ~/Library/LaunchAgents/com.github.facebook.watchman.plist
watchman version

https://github.com/facebook/watchman/issues/381#issuecomment-257673900

You should see something like this then you know it worked:

{
    "version": "2021.08.23.00"
}

Then re-run your jest tests

Zhong Huiwen
  • 889
  • 13
  • 10
  • 1
    I am wondering if you remember, how did you know it was related to watchman? – Matei Iorgulescu Dec 02 '21 at 14:54
  • 1
    @MateiIorgulescu i think because i had 2 MacBooks and one was running the tests fine, the other not. Both had the same node & node modules. The only difference was that on one, the watchman had a different version – Zhong Huiwen Dec 03 '21 at 15:30
  • This was my problem: watchman hanging on a Mac Book Pro M1; node@18 jest@29.3.1 – Jakob Hohlfeld Dec 21 '22 at 16:41
7

On MacOS I fixed this by uninstalling and reinstalling watchman using brew uninstall watchman and then reinstalling with brew install watchman. I had recently upgraded the OS so that may have something to do with it.

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
5

I had a similar issue. Most of my test where running but one suite was continuously running and never erroring.

It turned out I had a race condition in one of my useEffects. Which just cause jest to continuously run.

To diagnose the issue:

Comment out all test but one. keep adding a test till you find the one triggering the error.

Analyze the test to Identify which code is effected. (My issue turned out to be in a UseEffect.)

Identify which state object is causing the race condition.

Fix: remove it or throw a condition check on your code to prevent the race condition

Centerwork
  • 139
  • 2
  • 4
  • Instead of commenting out all other tests, you can just replace `it`, `test` or `describe` with `it.only`, `test.only` or `describe.only` to run just that test (or set of tests) in the suite. Alternatively, do the inverse and swap `only` for `skip`, to skip a single test. – JamieGL Feb 28 '22 at 20:32
  • 1
    Since it is temporary, I can select and comment much faster then find and replace. It's a time saver. – Centerwork Mar 01 '22 at 21:09
  • You only need to change `test` to `test.only` on the single test you want to run and it will automatically skip the rest. So it's just a one line change in the whole file, no need to touch any of the other tests. – JamieGL Mar 02 '22 at 06:55
  • Thanks, commenting out `useEffect`make it pass. Now have to figure out why it's happening. In my case `useEffect` calls `reset()` from 'react-hook-form' use form. – wviana May 04 '22 at 21:23
2

In my case it was because I was using a jest plugin for VS code and auto save was enabled. So, jest hanged after running too many times.

Removing the /tmp/jest_rs solved my problem.

Naveen Kulkarni
  • 633
  • 6
  • 16
2

I had the same issue, tests pass and then it hangs and the CI hangs too. I stumbled upon --forceExit, and it fixed it for me.

Dev Yego
  • 539
  • 2
  • 12
2

So I had a situation similar to this, only that jest refused to run any of my tests! The cause was a little convoluted: I hadn't run the project in over 6 months and various dependencies had picked up vulnerability issues. Back when I had last run the project and tests, I had jest installed globally with a global config. Then at one point, I had uninstalled jest from global (hence losing the global config) but didn't remember to add a local one to the project I'm talking about.

When debugging to try and get the tests to run, I nuked my node modules and did a fresh install.

Only after adding a jest.config with setting the no cache options did it eventually work out.

In summary:

  • If you don't have jest installed globally, make sure to have a jest config for your project.
  • After using the cli to set up the config, go back and manually check all the options you've chosen are there and modify the ones cli didn't prompt for.
2

Debug useEffect hook used inside components because some times useEffect creates infinite loop problems and the jest doesn't finishes the tests

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
rohit
  • 19
  • 1
2

For me the answers in this thread were not sufficient (watchmen, run-time flags, etc...), though the runtime flags did help me with debugging the issue.

For me the exact same problem was happening upgrading from Jest 26 to 27+.

The problem actually stemmed from using jest.useFakeTimers() in my jest setupFile. This was a workaround that I previously put there for react-navigate.

needed to remove the jest.useFakeTimers() configuration and make tests async, and sleep at the end of each test to allow it to tear down

const sleep = (ms: number) => new Promise((resolve) => setTimeout(() => resolve(true), ms));

afterAll(async () => {
  await sleep(10); // avoid jest open handle error
});

it('...', async () => {
Ryan Delao
  • 21
  • 3
  • Thank you so much, I also added jest.useFakeTimers() when I was trying to get testing working. – sprut Aug 17 '23 at 09:35
1

In my case, updating nodejs from an older version (ie. 12.16.1) to something more recent (eg. 14.17.3) and then re-installing jest (npm install jest -g) resolved this completely.

  • switching nodejs versions helped me. I'm using nvm and I was using v16, but switching to v14 worked – ahong Jun 06 '23 at 20:38
1

Using --maxWorkers option worked on my end. Jest will adapt to your maximum resources. Doesn’t matter how many resources you have. Jest will drain them all. You can limit the CPU use by using:

--maxWorkers=<num>|<string>

lissettdm
  • 12,267
  • 1
  • 18
  • 39
  • Thanks, it works! Previously, my laptop hung out when Node occupied all my 20 CPU cores: Intel® Core™ i9-12900H Processor (Total Cores 14 / Total Threads 20) OS: Ubuntu 22.04 – Oleksandr Bratashov Jun 30 '23 at 10:15
0

I have fixed this by modifying my .babelrc

{
 "presets": ["@babel/preset-env", "@babel/preset-react"],
 "plugins": [
   [
     "styled-jsx/babel",
     {
       // "plugins": ["styled-jsx-plugin-postcss"]
     }
   ]
 ]
}
groot
  • 420
  • 1
  • 7
  • 15
0

In my case I tried everything around internet and the only solution was to set isolatedModules: true (I explicitly set it to false).

Gabriel Llamas
  • 18,244
  • 26
  • 87
  • 112
0

I had something similar with the project on NestJS. It turned out that one of the modules contained an imported module, but it was not installed. And for some inexplicable reason, there was no information about this when the tests were launched.

0

check your tests, in my case I mistake and put an extra "a" on the test

  test('test expected that ..', async (a) => {
...

this makes my test hang forever (using node 14.15 and jest 29.1.2).

Cristian Zumelzu
  • 842
  • 10
  • 15
0

The @rohit's answer has given me the clue to find the solution to my problem, my code was working fine, but jest was failing into an infinite loop with the useEffect

so I've solved making a mock of it

import React from 'react'

const mockUseEffect = jest.fn()

jest.spyOn(React, 'useEffect').mockImplementation(mockUseEffect)

you can adapt it as your needs

CrsCaballero
  • 2,124
  • 1
  • 24
  • 31
0

I had the same issue, tests never started, timer counted up indefinitely. None of the suggested solutions worked.

It turned out I needed to remove the --experimental-vm-modules node flag from the command line, which was in use with Node v16, but caused this issue after an update to Node v20.

Hope it may help someone.

Bence Szalai
  • 768
  • 8
  • 20