Unable to run Jest unit tests when ErrorBoundary is imported
See original GitHub issueExpected 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:
- Created 5 years ago
- Comments:13 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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 workTo those that come across this ticket in the future, the following worked for me:
It is a slightly modified version of the mock provided above, but it returns a function instead of an object
@dbchristopher