Skip to content

Support jest globalSetup #4393

Closed
Closed

Description

@DuskLoop

Is this a bug report?

No

Problem

I have some setup code that I want to run once before all tests. If I run the code in setupTests.js it will run before every test which slows down the total test time alot.

Jest seems to have an option for this: https://facebook.github.io/jest/docs/en/configuration.html#globalsetup-string
But when I add this option in my package.json jest config section I get message:
These options in your package.json Jest configuration are not currently supported by Create React App:

• globalSetup

Would be nice to see this option supported by Create React App or if someone could provide a workaround for this issue.

Activity

  1. Stupidism commented on May 3, 2018

    @Stupidism

    @DuskLoop I think react-app-rewired can help you modifying jest config without ejecting

  2. gaearon commented on May 3, 2018

    @gaearon
    Contributor

    Maybe it would be sufficient to use a global flag in src/setupTest.js?

    if (!global.initializedTests) {
      // do something
      global.initializedTests = true;
    }

    I'm not 100% sure but I think global variables should persist between tests.

  3. DuskLoop commented on May 4, 2018

    @DuskLoop
    Author

    @gaearon global does not seem to persist between test files.

    This code:

    if (!global.initializedTests) {
      console.log("doing something");
      global.initializedTests = true;
    }
    

    prints for every test file and no difference in performance

  4. DuskLoop commented on May 4, 2018

    @DuskLoop
    Author

    @Stupidism I tried running tests with react-app-rewired only to find out that react-scripts currently uses jest 20.0.4, the globalSetup option is supported in jest 22.

    Looks like react-scripts is on it's way to update their jest dependency though, the next branch uses version 22.4.1.

  5. Brian-McBride commented on Oct 1, 2018

    @Brian-McBride

    This is an easy change in one file: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/utils/createJestConfig.js

    Current:

      const supportedKeys = [
        'collectCoverageFrom',
        'coverageReporters',
        'coverageThreshold',
        'resetMocks',
        'resetModules',
        'snapshotSerializers',
        'watchPathIgnorePatterns',
      ];
    

    Changed:

      const supportedKeys = [
        'collectCoverageFrom',
        'coverageReporters',
        'coverageThreshold',
        'globalSetup',
        'globalTeardown',
        'resetMocks',
        'resetModules',
        'snapshotSerializers',
        'watchPathIgnorePatterns',
      ];
    

    One the keys are allowed to pass through, you can just specify these in your package.json jest:{} entry (or wherever).

  6. gaearon commented on Oct 1, 2018

    @gaearon
    Contributor

    That sounds fine to support out of the box. I'll add it.

  7. pakaplace commented on Nov 6, 2018

    @pakaplace

    @gaearon , curious as to why certain Jest config options are supported and others are not? I want to use the Jest preset config option, in order to use the jest-puppeteer module with my create-react-app. Could you add it as a supportedKey?

  8. patricklafrance commented on Nov 7, 2018

    @patricklafrance

    @pakaplace if you customize your CRA with craco you could do it.

    Here's a sample craco.config.js file:

    module.exports = {
        jest: {
            configure: {
                 preset: "jest-puppeteer"
            }
        }
    };

    Hope this helps!

  9. locked and limited conversation to collaborators on Jan 9, 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