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)

Most upvoted comments

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!

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"
    }
  }

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 the jest.config.js maps to via setupFilesAfterEnv).

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!

setTimeout().__proto__.unref = function () {}

Please use the browser: true option in Jest:

Respect Browserify’s “browser” field in package.json when resolving modules. Some modules export different versions based on whether they are operating in Node or a browser.

In its package.json @bugsnag/js uses the browser field to export the browser bundle and the main field 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 the browser field 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.

Test suite failed to run
• TypeError: setInterval(...).unref is not a function

Node v11.6.0 npm v6.5.0 Bugsnag v5.1.0

Running npm test from 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):

{
  "name": "my-package",
  "version": "1.0.0",
  "jest": {
    "testEnvironment": "node"
  }
}

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 testEnvironment docs 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: true needs to be under the jest key package.json, can you confirm it was there?

{
  "name": "my-package",
  "version": "1.0.0",
  "jest": {
    "browser": true
  }
}

I ended up doing something like this to stub out bugsnag within the test environment:

const bugsnag = require('@bugsnag/js');
const bugsnagExpress = require('@bugsnag/plugin-express');

let bugsnagClient;

if (process.env.NODE_ENV !== 'testing') {
  bugsnagClient = bugsnag('xxxxxx');
  bugsnagClient.use(bugsnagExpress);
} else {
  bugsnagClient = {
    getPlugin() {
      return {
        requestHandler(req, res, next) {
          req.bugsnag = { notify() {} };
          return next();
        },
        errorHandler(req, res, next) {
          return next();
        },
      };
    },
  };
}

Also using jest to 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 the package.json file 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)