Jest: Jest loops in watch mode

Created on 8 Dec 2017  ·  23Comments  ·  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
bug

What is the current behavior?
Jest --watch loops

loop

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

Mine is a rewired CRA with these Jest options:

"jest": {
    "setupFiles": [
      "raf/polyfill"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 30,
        "functions": 34,
        "lines": 39,
        "statements": 34
      }
    },
    "testResultsProcessor": "jest-bamboo-formatter",

What is the expected behavior?

Jest watch shouldn't loop

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Node v8.9.0
Yarn 1.3.2
Hest 20.0.4

Needs More Info

Most helpful comment

Solved in jest.config.js by

    roots: ['src']

The problem for me was due to files created by reporters, which trigger a new test run everytime the previous run created the report file.
Another solution was to ignore report files with watchPathIgnorePatterns

All 23 comments

Can you send a repo with the issue happening?

I can't share the repo and I fear there's something in my repo that's causing this problem...

I thought it could be this (https://github.com/facebook/jest/issues/4635), but I tried the suggested fix and it doesn't help 🤷‍♀️

Can you profile if anything is triggering a change in your FS. How it behaves it there are tests, or you press a or run with --watchAll?

@thymikee may you provide a source where I can read how to profile the FS changes?

With --watchAll I get the same problem. If I run all the tests, they run, and then they automatically run again

I see that jest.json is changing, but I already excluded it with:

    "watchPathIgnorePatterns": [
      "<rootDir>/jest.json"
    ],

It's really hard to tell something without the code in front of you. I would try other patterns to ignore (e.g. without rootDir, using absolute path, more generic, whatever) and monkey-patch setting watchPathIgnorePatterns to see what it gets eventually

I can't find the watch file to monkey patch, where is it?

I tried with require.resolve('./jest.json') but it still loops

What I did was add a console.log to the change event linked in that issue. Then you can see what file triggers the watcher

The file is jest.json, but I should have already ignored it 🤷‍♀️

Just that file? Without a repro it's hard to help you...

Yeah I know... for now I managed to enable the bamboo reporter only when the tests run on CI, so that I don't encounter this problem, I guess it's also a good thing because doing so I don't run useless code on local machines.

Closing as there's not much we can do without a repro. Feel free to keep bouncing ideas here!

I experience this problem with a fairly vanilla create-react-app learning project. See https://github.com/susiehill/amortization for the repo. I tried the workaround in #4635, which first required that I do an eject, but that did not appear to work.

This problem appears intermittently. _Sometimes_, blowing away node_modules and redoing the npm install fixes the problem, but only temporarily--it seems to invariably resurface later.

At least once, it sat there when I started (because it didn't need to re-rerun tests from the last run), but the problem started when I pressed a.

MacOS 10.13.3
Jest 20.0.4 (as installed via create-react-app)
Node 8.2.1
NPM 5.7.1

So what's particularly interesting is that I had to work on a Windows machine (I think v10) earlier, and had experienced a different problem there: A single test takes excessive time (12-20s) to run. There are a number of posts on that issue, similarly, but no fix worked. Any chance these are related problems?

I experience this problem when I use ../node_modules/.bin/jest to run, Loop execution does not stop,I try and add --env=node ,

I just started experiencing the same issue and have been scratching my head trying to find a solution… and I’ve figured it out. __My root directory was in my Dropbox folder.__ Every time Dropbox sync’d a file (even upload, it appears), Jest detected a change and reran the test. Pausing Dropbox sync fixes this for me.

I was getting this error, just after create an app with create-reac-app. The project was in a folder sync with iCloud too. I've moved the project to the desktop folder and the problem stopped. Seems me that jest is watching some folder above and triggering the tests.

Solved by

    "watchPathIgnorePatterns": [
      "<rootDir>/node_modules",
    ]

although I don't believe it to be the right solution.

Solved in jest.config.js by

    roots: ['src']

The problem for me was due to files created by reporters, which trigger a new test run everytime the previous run created the report file.
Another solution was to ignore report files with watchPathIgnorePatterns

What I did was add a console.log to the change event linked in that issue. Then you can see what file triggers the watcher

Can you provide an example of how to do this?

For me it was a bit of a combination of both 951565664's comment & DenisGorbachev's solution.

We start jest from within a script that imports Jest itself, so it pulls jest out of "/node_modules/.bin/jest". Not 100% why, but running jest out of "node_modules" seems to update at least one file over and over. So, when using --watchAll it'll loop 5+ times before finally settling.

However, we can't use 951565664's solution of --env=node (or "testEnvironment": "node" for the config JSON), as some of our tests require the virtual DOM provided by jsdom.
But, adding node_modules to the watchPathIgnorePatterns Denis mentioned fixed it.

Was this page helpful?
0 / 5 - 0 ratings