Skip to content

Allow npm test to not run in watch mode for better cross-platform / tooling CI. #1137

Closed

Description

@cchamberlain

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).

Activity

  1. fson commented on Dec 3, 2016

    @fson
    Contributor

    @cchamberlain Could you elaborate on why setting the CI environment variable is not an option for you? Would cross-env help here?

  2. cchamberlain commented on Dec 6, 2016

    @cchamberlain
    Author

    @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?

  3. troygoode commented on Jan 2, 2017

    @troygoode

    @cchamberlain I've just changed my package.json to:

    "test": "CI=true react-scripts test --env=jsdom",
    

    which works great

  4. cchamberlain commented on Jan 3, 2017

    @cchamberlain
    Author

    @troygoode - Have you tried that on Windows?

  5. troygoode commented on Jan 3, 2017

    @troygoode

    @cchamberlain no, I haven't. works great in bash though

  6. cchamberlain commented on Jan 3, 2017

    @cchamberlain
    Author

    @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.

  7. gaearon commented on Feb 11, 2017

    @gaearon
    Contributor

    You can set them in cross-platform way:

    npm i --save-dev cross-env
    

    then use

      "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!

  8. ryansully commented on Feb 12, 2017

    @ryansully
    Contributor

    From https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/scripts/test.js#L29-L32

    // Watch unless on CI or in coverage mode
    if (!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.

  9. cchamberlain commented on Feb 13, 2017

    @cchamberlain
    Author

    Thanks @gaearon and @ryansully. Both of those should cover my needs.

  10. quantuminformation commented on Apr 22, 2017

    @quantuminformation

    Did anyone get this to work with pre-commit? I'm getting passing tests but the exit code stops the commit.
    I get:

    We've failed to pass the specified git pre-commit hooks as the `npm`
    			pre-commit: hook returned an exit code (1)
    

    image

  11. gaearon commented on Apr 22, 2017

    @gaearon
    Contributor

    If you can create a standalone reproducing case somebody might be able to take a look!
    (Please file a new issue for this.)

  12. added a commit that references this issue on Oct 13, 2017
  13. antonovicha commented on Jul 20, 2018

    @antonovicha

    and probably others already do this.

    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.

  14. locked and limited conversation to collaborators on Jan 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions