Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support jest globalSetup #4393

Closed
DuskLoop opened this issue May 2, 2018 · 8 comments
Closed

Support jest globalSetup #4393

DuskLoop opened this issue May 2, 2018 · 8 comments

Comments

@DuskLoop
Copy link

DuskLoop commented May 2, 2018

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.

@Stupidism
Copy link

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

@gaearon
Copy link
Contributor

gaearon commented May 3, 2018

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.

@DuskLoop
Copy link
Author

DuskLoop commented May 4, 2018

@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

@DuskLoop
Copy link
Author

DuskLoop commented May 4, 2018

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

@Brian-McBride
Copy link

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

@gaearon
Copy link
Contributor

gaearon commented Oct 1, 2018

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

@pakaplace
Copy link

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

@patricklafrance
Copy link

patricklafrance commented Nov 7, 2018

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

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants