-
Notifications
You must be signed in to change notification settings - Fork 253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot instantiate bugsnag client in jest test #452
Comments
This seems odd – are your jest tests running in Node? You're in a bit of a grey area because jest tests usually run browser code in node, and this means that Bugsnag loads What version of Node are you running? There might be something we can do here – either to get it to load the browser notifier (depending on how much of the browser env gets stubbed/mocked by jest) or make the Node notifier behave a little better. In order to do so it would be good to have a bit more context or info about your setup. Please provide as much as you can! A stripped back project that we can run and reproduce this issue would be perfect. Thanks! |
Let me know if you need any more details. I'll let you know what I have a stripped back project to test against. |
Repro is here https://github.com/keattang/bugsnag-bug-repro-2 |
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 () {} |
Great thanks! |
Any updates on this matter?
Node v11.6.0 Running |
+1 for this issue while running Jest in a Puppeteer environment. Node v8.12.0 |
Please use the
In its We'll still fix this error because it's confusing and unnecessary, but it's important that Jest loads the correct module! |
I'm also experiencing this error. I tried setting the browser setting in package.json but without any success. However I have a Nextjs setup so it is possible those settings override that at some point. In the end I don't know if this is an optimal solution for me anyway since I have both Node and "browser" tests for my server side rendering process and is dependant on both the node and browser version of Bugsnag. |
{
"name": "my-package",
"version": "1.0.0",
"jest": {
"browser": true
}
} |
Yes unfortunately that was the case. I also tried playing around with using jests environment setting in the test file but with the same result
Result
I use Jest 24.40 and Bugsnag 6.0.0 |
Same issue here using CRA ( |
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 |
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 @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! |
Oh and here's a link to said PR facebook/create-react-app#7024 😄 |
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 Also consider if Bugsnag should be loaded in your tests at all! If you are testing the 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! |
👍
works for me in project and testing(using jest and enzyme) as well. |
@bengourley this still happens on node with @bugsnag/js if you follow the instructions on the bugsnag website jest won't run |
@andreimc - have you tried the suggestion Ben made previously here yet? |
Adding |
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:
|
Another way to do this, using moduleNameMapper in module.exports = {
moduleNameMapper: {
'@bugsnag/js': '@bugsnag/browser'
}
} (or package.json "jest": {
"moduleNameMapper": {
"@bugsnag/js": "@bugsnag/browser"
}
} |
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:
The error occurs when trying to create the client with
Bugsnag({...})
I get the following stack trace:
The text was updated successfully, but these errors were encountered: