You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow npm test to not run in watch mode for better cross-platform / tooling CI. #1137
I've been using create-react-app in conjunction with lerna repos and for the most part its been an awesome experience. The one issue I've been seeing is when trying to issue lerna run test commands to test all packages that have a "test" script. Since create-react-app defaults to entering watch mode on test, it hangs.
After some digging it looks like I might be able to set a CI environment variable to disable the watch mode, however this does not work well due to the nuances of setting environment variables cross platform.
Something as simple as changing the default from
"test": "react-scripts test --env=jsdom",
to
"test": "react-scripts test --env=jsdom --watch",
would allow me to simply turn it off. Conversely adding an optional --no-watch or --single-run flag would solve the problem.
I'm happy to submit a PR, but wanted to get some feedback first on the internal direction regarding whether this has been considered in the past, and the reasoning around the current implementation. I get that its a nice experience for the casual user, but at the cost of CI and incompatibility with lerna there should be some sort of escape hatch (short of eject).
Reacted by Nathan Phillip Brink, Bruno Casali, Matteo Codogno, Bob Aman, miguel, Dom Ginger, Artem Sapegin, Alistair MacDonald, Kip Obenauf, Constantin Lungu and 12 more
@fson - Thanks, I'll test it out soon but think it should work.
Still feels against the norm to me for npm test to run tests in watch mode. Given how common it is for CI to run npm test, wouldn't it be simpler to have that just execute the tests and have users run npm test -- --watch to run jest in watch mode?
Reacted by Joe Bowbeer, Nick Heiner, Emanuele Ianni and Jarrett Horton
Reacted by Amio Jin, Diego Sisto, Anton Putra, Mahesh J, Alex White, Roman Nesterov, miguel, Farzad Yousefzadeh, Mike Staub, Sam Joseph and 56 more
Reacted by miguel, Farzad Yousefzadeh, Sam Joseph, Dan Seethaler, Kyubin Han, Alexandre Rivest, Muhsin Keloth, Hector Merla, Piotr Ferens, Alejandro and 11 more
@troygoode - The main problem here is that setting environment variables via scripts section is not cross-platform friendly and is not on a roadmap to ever be (last I read).
On Windows, you would need to use: "test": "set CI=true &&react-scripts test --env=jsdom",
Further, npm test is commonly used in CI processes in the context of it being a single run without needing to set an environment variable. This is the only thing I've come across in create-react-app that rubs me the wrong way. As @fson pointed out, I could use cross-env to work around this but no workaround would be necessary if npm test ran tests once, and npm test -- --watch did the same thing I'd expect jest --watch does. This approach would also allow create-react-app to continue with its no dependencies approach since it's baked into react-scripts.
Reacted by antonovicha, Lam Vuong, Justin D. Harris and Salathiel Genese
"test": "cross-env CI=true react-scripts test --env=jsdom"
Further, npm test is commonly used in CI processes in the context of it being a single run without needing to set an environment variable.
I think you might have misunderstood. In most CIs you don't need to set this variable because it is already set by CI environments. Travis, Circle, and probably others already do this.
I hope this helps!
Reacted by Joel Aguero, Diabolus ex machina, Christophe Lazantsy, Matteo Codogno, Kunal, Mark Palfreeman, Mario Costa Junior, Brandon J. Boone, s0ld13r, Vishnu kumar and 16 more
Reacted by Andrew Backes and Volodymyr Bergman
Reacted by Cole Chamberlain, Aaron Stacy, Mahesh J, Jan Killian, Alex White, Joel Aguero, Christophe Lazantsy, Thiago Nogueira, Glogo, Mateusz Wozniak and 6 more
// Watch unless on CI or in coverage modeif(!process.env.CI&&argv.indexOf('--coverage')<0){argv.push('--watch');}
From this, cross-env is one solution, but running npm test -- --coverage also only runs tests once, and avoids the need for another dependency (unless you already have need for it for other reasons).
For my projects I've created an additional NPM script:
"test:coverage": "npm test -- --coverage"
and just include that in other script definitions as needed when I want to run tests only once.
Reacted by Cole Chamberlain, Andrea Arcuri, Ricky N, Samuel Huang, John Gill, Jan Molak, Joe Bowbeer, aleksandr-kirillov, Guilherme Porto, Paul Li and 4 more
Reacted by John Gill
Reacted by John Gill, Jan Molak, Michael Ovchinnikov and Volodymyr Bergman
Activity
fson commentedon Dec 3, 2016
@cchamberlain Could you elaborate on why setting the
CIenvironment variable is not an option for you? Would cross-env help here?cchamberlain commentedon Dec 6, 2016
@fson - Thanks, I'll test it out soon but think it should work.
Still feels against the norm to me for
npm testto run tests in watch mode. Given how common it is for CI to runnpm test, wouldn't it be simpler to have that just execute the tests and have users runnpm test -- --watchto run jest in watch mode?troygoode commentedon Jan 2, 2017
@cchamberlain I've just changed my
package.jsonto:which works great
cchamberlain commentedon Jan 3, 2017
@troygoode - Have you tried that on Windows?
troygoode commentedon Jan 3, 2017
@cchamberlain no, I haven't. works great in bash though
cchamberlain commentedon Jan 3, 2017
@troygoode - The main problem here is that setting environment variables via scripts section is not cross-platform friendly and is not on a roadmap to ever be (last I read).
On Windows, you would need to use:
"test": "set CI=true &&react-scripts test --env=jsdom",Further,
npm testis commonly used in CI processes in the context of it being a single run without needing to set an environment variable. This is the only thing I've come across in create-react-app that rubs me the wrong way. As @fson pointed out, I could use cross-env to work around this but no workaround would be necessary ifnpm testran tests once, andnpm test -- --watchdid the same thing I'd expectjest --watchdoes. This approach would also allow create-react-app to continue with its no dependencies approach since it's baked into react-scripts.gaearon commentedon Feb 11, 2017
You can set them in cross-platform way:
then use
I think you might have misunderstood. In most CIs you don't need to set this variable because it is already set by CI environments. Travis, Circle, and probably others already do this.
I hope this helps!
ryansully commentedon Feb 12, 2017
From https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/scripts/test.js#L29-L32
From this,
cross-envis one solution, but runningnpm test -- --coveragealso only runs tests once, and avoids the need for another dependency (unless you already have need for it for other reasons).For my projects I've created an additional NPM script:
and just include that in other script definitions as needed when I want to run tests only once.
cchamberlain commentedon Feb 13, 2017
Thanks @gaearon and @ryansully. Both of those should cover my needs.
quantuminformation commentedon Apr 22, 2017
Did anyone get this to work with pre-commit? I'm getting passing tests but the exit code stops the commit.
I get:
gaearon commentedon Apr 22, 2017
If you can create a standalone reproducing case somebody might be able to take a look!
(Please file a new issue for this.)
Run tests without user input
antonovicha commentedon Jul 20, 2018
Jenkins does not set CI env variable.
Also setting up debug launch configuration in VS Code for tests is extra effort. Actually I am not yet figure it out.
add --coverage to test to force single run