bugsnag-js: Cannot instantiate bugsnag client in jest test
I have a react app and I am trying to test one of my components that pulls in my bugsnag client and uses it to create an ErrorBoundary using the react plugin.
However, when creating the bugsnag client I get the following error: TypeError: setInterval(...).unref is not a function.
The file the creates the client looks like this:
import React from 'react';
import Bugsnag from '@bugsnag/js';
import bugsnagReact from '@bugsnag/plugin-react';
import config from '../config';
const bugsnag = Bugsnag({
apiKey: config.bugsnagKey,
releaseStage: config.env,
appVersion: config.version,
notifyReleaseStages: ['production', 'staging'],
});
bugsnag.use(bugsnagReact, React);
export default bugsnag;
The error occurs when trying to create the client with Bugsnag({...})
I get the following stack trace:
at SessionTracker.start (node_modules/@bugsnag/node/dist/bugsnag.js:2138:75)
at Object.init (node_modules/@bugsnag/node/dist/bugsnag.js:2028:20)
at BugsnagClient.use (node_modules/@bugsnag/node/dist/bugsnag.js:163:30)
at node_modules/@bugsnag/node/dist/bugsnag.js:1578:20
at Array.forEach (<anonymous>)
at module.exports (node_modules/@bugsnag/node/dist/bugsnag.js:1577:11)
at Object.<anonymous> (src/utilities/bugsnag.js:91:9)
at Object.<anonymous> (src/index.jsx:503:35)
at Object.<anonymous> (test/components/App.spec.jsx:9:35)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 23
- Comments: 22 (8 by maintainers)
Another solution that I haven’t yet mentioned is that if your code is not “universal” i.e. you don’t run the same thing in Node and in the Browser, and you don’t need to automatic detection of whether to load the Node/Browser notifier, you can import the exact notifier you want:
So if your tests are running in jsdom, pull in
@bugsnag/browserand if they are running in Node, pull in@bugsnag/node.Also consider if Bugsnag should be loaded in your tests at all! If you are testing the
ErrorBoundarycomponent with Jest then that seems a good enough reason, but if you aren’t using any of the features and are just switching off error reporting, maybe the scope of your tests should be reduced to not include an error reporter?I’m going to close this off as there are multiple workable solutions. The fact that you get an error at all is a good canary that the wrong library has been loaded, and hopefully anybody who gets this error in future will find this issue.
Any further problems, let us know!
Another way to do this, using moduleNameMapper in
jest.config.js:(or package.json
jestfield):Agreed on not needing Bugsnag during tests, but documentation usually instantiates Bugsnag globally or as an IIFE. To avoid caring about Bugsnag during tests while following Bugsnag docs, simply add:
jest.mock('@bugsnag/js');in your Jest setup file (the file that thejest.config.jsmaps to viasetupFilesAfterEnv).Thanks for the repro, very helpful! We will take a look. Until we come back with a solution, I would suggest to following awful temporary monkeypatch in your tests!
Please use the
browser: trueoption in Jest:In its
package.json@bugsnag/js uses thebrowserfield to export the browser bundle and themainfield to export the Node bundle. Jest is weird because it looks like Node but runs tests in a browser-like jsdom environment. I don’t know why it doesn’t respect thebrowserfield by default.We’ll still fix this error because it’s confusing and unnecessary, but it’s important that Jest loads the correct module!
Any updates on this matter? Experiencing the same issue.
Node v11.6.0 npm v6.5.0 Bugsnag v5.1.0
Running
npm testfrom a CRA project. The test suite is quite simple because it only checks if a component renders.Thanks @domharrington! If you’re running Node tests – and you’re not doing this already – you should make sure Jest is using the Node environment because by default it uses jsdom. Set this with something like (or command line flag, or @ test annotation):
Though if you’re using Jest for Node and browser tests in the same codebase you’d probably want to set it dynamically. See the
testEnvironmentdocs for more details.@axelinternet can you share more information on your setup or provide a sample repo where I can reproduce the problem? Is your code being bundled or built before your tests are run?
@remotealex I’ve filled a PR against create-react-app to allow you to set the correct config, feel free to 👍 that issue so that it gets more attention!
browser: trueneeds to be under thejestkeypackage.json, can you confirm it was there?I ended up doing something like this to stub out bugsnag within the test environment:
Also using
jestto run node.js tests.+1 for this issue while running Jest in a Puppeteer environment.
Node v8.12.0 npm v6.5.0 Bugsnag v5.1.0 Jest v23.1.0 Puppeteer v1.11.0
Adding
testEnvironment: "node"to my jest.config.js solved my problem. Adding it to thepackage.jsonfile did not work.👍
import bugsnag from '@bugsnag/browser';works for me in project and testing(using jest and enzyme) as well. 👍
Same issue here using CRA (
--typescript)