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

Update default jest config to browser:true #7024

Closed
wants to merge 2 commits into from

Conversation

bengourley
Copy link

Hi! I work at Bugsnag maintaining the notifier for JavaScript @bugsnag/js. Some of our users have reported issues when using Bugsnag in Jest tests.

We use the package.json#browser field to provide the correct Node or Browser module based on the environment so that "universal" JS can simply import bugsnag from '@bugsnag/js' and get the right one.

For some reason, even though the default Jest test environment is jsdom (not Node) the default behaviour is to ignore the browser field in package.json and load the Node version of a module.

In the issue, I've advised people to set browser: true in their Jest config, but when you do this on a create-react-app it fails with the following error:

scratch/reaccct - [inline-script-improvement] » npm test

> reaccct@0.1.0 test /Users/bengourley/Development/bugsnag-js/packages/node/scratch/reaccct
> react-scripts test


Out of the box, Create React App only supports overriding these Jest options:

  • collectCoverageFrom
  • coverageReporters
  • coverageThreshold
  • extraGlobals
  • globalSetup
  • globalTeardown
  • resetMocks
  • resetModules
  • snapshotSerializers
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • browser

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

Ideally, I would like to get Jest to change the default config to be browser: true – since after all the default environment is a browser-like environment. However, users are able to correct this config themselves. My justification for this change to create-react-app is that users are not allowed to override the config at all, and so can't get the tests to load the correct version of our module.

Since I'm setting this to something other than the default, and jestEnvironment is also configurable, I've added browser to the list of supportedKeys, but that can be up for discussion.

Thanks for taking a look 🙂

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@ianschmitz ianschmitz closed this May 4, 2020
@ianschmitz ianschmitz reopened this May 4, 2020
@ianschmitz
Copy link
Contributor

Thanks for the PR @bengourley. We're just updating to Jest 26, and this property was removed: jestjs/jest#9943.

@ianschmitz ianschmitz closed this May 4, 2020
@ianschmitz ianschmitz removed this from the 4.0 milestone May 4, 2020
@bengourley
Copy link
Author

Is it possible to specify a custom jest resolver via create react app? It seems like we're getting further away from a solution 😞

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

Successfully merging this pull request may close these issues.

5 participants