Created by: bengourley
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