question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot instantiate bugsnag client in jest test

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:22
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

11reactions
bengourleycommented, May 13, 2019

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:

const bugsnag = require('@bugsnag/browser')
// or
const bugsnag = require('@bugsnag/node')

So if your tests are running in jsdom, pull in @bugsnag/browser and 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 ErrorBoundary component 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!

7reactions
evocateurcommented, Jun 21, 2021

Another way to do this, using moduleNameMapper in jest.config.js:

module.exports = {
  moduleNameMapper: {
    '@bugsnag/js': '@bugsnag/browser'
  }
}

(or package.json jest field):

  "jest": {
    "moduleNameMapper": {
      "@bugsnag/js": "@bugsnag/browser"
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to mock Bugsnag in jest test cases for a react app
Here is an example of how you can get this working: jest.mock('bugsnag-js', () => ( () => ({ use(plugin) { const boundary =...
Read more >
React integration guide - JavaScript - Bugsnag docs
To start Bugsnag with the React integration, instantiate the BugsnagPluginReact and pass it along with your API key to Bugsnag.start as configuration:
Read more >
typeerror: cannot read properties of undefined (reading 'default')
Apparently react-test-renderer fails to instantiate Components if they are missing their constructor. So I needed to add them following line to my PureComponent ......
Read more >
Ecosystem - Fastify
Fastify MongoDB in Memory Plugin for testing support. fastify-mongodb-sanitizer, Fastify plugin that sanitizes client input to prevent potential MongoDB query ...
Read more >
Open Source Used In Socio 1.0 - Cisco
1.19 testing-library/jest-dom 5.11.6 ... 1.170 bugsnag/safe-json-stringify 2.1.0 ... that such additional attribution notices cannot be construed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found