Running CRA Jest in non-interactive mode

ReactjsReduxJestjsCreate React-App

Reactjs Problem Overview


Update: my use case is mainly to run tests at CI, but overriding default CRA Jest parameters is something I'm generally wondering about.


I'm running tests using the Jest, config that came with Create React App. It always launches into the interactive mode:

 › Press a to run all tests.
 › Press o to only run tests related to changed files.
 › Press p to filter by a filename regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

But I don't want it to wait for my input. I want it to run once and then terminate. I tried using the --bail or --no-watchman switches but it still launches in interactive mode.

If I globally install jest, and run it in the root of my project, it executes once and finish (just as I want). But when I run npm test which runs react-scripts test, it goes into the watch mode even when I'm not passing --watch.

Update: I've also filed an issue on CRA.

Reactjs Solutions


Solution 1 - Reactjs

You should use Jests --watchAll=false flag.

eg:

npm test -- --watchAll=false

Note: this is for react-scripts > 3.00

For older versions:

  • react-scripts >= 2.1.4 < 3.00

For non-ci, eg running tests locally, you can pass a --no-watch flag:

npm test --no-watch

  • react-scripts <= 2.1.3

CRA looks for a CI environment variable, if its present it doesn't run in watch mode.

CI=true npm test should do what you are looking for

See the User Guide -> Running Tests -> On your own environment

Solution 2 - Reactjs

In your package.json scripts:

"test": "react-scripts test --watchAll=false"

Or npm test -- --watchAll=false

Or yarn test --watchAll=false

Note: the flag used to be called --no-watch in react-scripts < 3.0: https://github.com/facebook/create-react-app/blob/3.x/CHANGELOG.md#remove---no-watch-flag

Solution 3 - Reactjs

non-interactive solution:

npm test a --watchAll=false

or

yarn test a --watchAll=false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexStackView Question on Stackoverflow
Solution 1 - ReactjsWayneCView Answer on Stackoverflow
Solution 2 - ReactjsthisismydesignView Answer on Stackoverflow
Solution 3 - ReactjsmsahinView Answer on Stackoverflow