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.

Unable to run Jest unit tests when ErrorBoundary is imported

See original GitHub issue

Expected behavior

I’m trying to add more granular error boundaries, rather than merely having a single wrapper around the app root. This is per React 16 intention to allow for catching errors without unmounting the entire app.

Observed behavior

It works fine in browser, but my Jest test suite won’t run when bugsnag client is imported.

This error is output by Jest test runner:

  ● Test suite failed to run

    TypeError: (0 , _bugsnagJs2.default) is not a function

      at Object.<anonymous> (src/app/initializers/bugsnag.js:17:43)
      at Object.<anonymous> (src/app/components/ErrorBoundary/ErrorBoundary.jsx:38:16)
      at Object.<anonymous> (src/app/components/ErrorBoundary/index.js:7:22)

  console.error node_modules/bugsnag-js/node/index.js:1
    +-------------------------------------------------------------+
      Bugsnag Error: "bugsnag-js" is for browser JavaScript only.
      For node, use the "bugsnag" package.
    +-------------------------------------------------------------+``

Steps to reproduce

Import ErrorBoundary into any component with a unit test.

Version

1.1

Additional information

[Insert any additional information]

Can’t comment on Issues?

Some users have been unable to comment on Github issues when an adblocker extension is enabled. We recommend temporarily disabling the extension, or if that fails, contacting support@bugsnag.com.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
foxyblockscommented, May 2, 2018

Hmm, sorry about that, bugsnag-js needs to run in a browser environment and jest runs tests in a node environment. We should figure out a recommended workaround, but in the mean time one thing you could try is to stub the error boundary in your jest tests to render a dumb pass through component. I think the syntax would be

jest.mock('src/app/components/ErrorBoundary', () => ({children}) => children)

You can also add this to your global setup file to have it run before every test.

If you don’t want to mock the error boundary you can mock the bugsnag-js module itself but that will require a more complicated mock object. I haven’t tested this but I think it would work

jest.mock('bugsnag-js', () => ({
  use (plugin) {
     const boundary = plugin.init()
     // we don't want the error boundary to swallow the errors, we want jest see them and fail the test
     delete boundary.prototype.componentDidCatch;
     return boundary;
  }
}))
2reactions
kcebcommented, Sep 4, 2018

To those that come across this ticket in the future, the following worked for me:

jest.mock('bugsnag-js', () => (
  () => ({
    use(plugin) {
      const boundary = plugin.init();
      delete boundary.prototype.componentDidCatch;
      return boundary;
    },
  })
));

It is a slightly modified version of the mock provided above, but it returns a function instead of an object

@dbchristopher

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to run Jest unit tests when ErrorBoundary is imported
Unable to run Jest unit tests when ErrorBoundary is imported.
Read more >
Error when importing a react module when using either ...
Currently, I'm trying to unit test some react components, ... \mpx-error-boundary\ErrorBoundary.js:2 import * as React from 'react'; ...
Read more >
How to make a Jest Test for Error Boundaries
The following is an example of how to create the test: import { render, screen } from '@testing-library/react'; import ErrorBoundary from './ ...
Read more >
How to make a Jest Test for Error Boundaries
Basically, an error that occurs within the error boundary propagates to said error boundary to ... import { render, screen } from '@testing-library/react';...
Read more >
Testing Next.js apps with Jest
Jest makes testing large Next.js apps a breeze with automated ... tests will run automatically and you'll be notified of any failed tests....
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