-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
"DeprecationWarning: Unhandled promise rejections are deprecated" #5311
Comments
I think the issue here is that we clean up our Not sure how to best handle it. I think maybe @aaronabramov's original suggestion in #3251 (comment) Or we could try to be smarter about attaching and detaching the listener, similar to how you solved #4767. |
I noticed something that I believe might be related to this. I ejected from create-react-app to get to the testing scripts.
That works and exits the test suite if I add a promise rejection into a test. When I upgrade Jest from |
I don't think our intention was to exit the test suite btw (that sounds pretty annoying). The only reason we added that line is to make sure errors in our scripts (not in the actual tests!) crash early. |
@gaearon But if what you are asking about is the case
Then wouldn't it be desired to crash the suite. Since on a remote it would crash the whole server? I'll need to google and see if in the future uncaught rejections will truly exit the process. It reads that way |
D:\ANGUALRJS2 PROJECTS\PR02>ngh (node:4020) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Unspecified error (run without silent option for detail) |
Same issue throwing an error asynchronously, see #5911 for a reproduction |
edit: Actually this workaround doesn't work. postYou can instruct Jest to wait for the promise to resolve using `expects().resolves`, which means Jest won't pack up the unhandledRejection handler until that promise has resolved or rejected:test("I'm asynchronous", () => { // not async
const promise = Promise.reject("boom!")
expect("some precondition").toBeFalsy()
const pending = rejected(promise) // Assign to variable if necessary
expect("some postcondition").toBeTruthy()
expect(pending).resolves.toEqual(expect.anything()) // Wait for promise to resolve
})
async function rejected(promise) {
try {
await promise
} catch (e) {
return e
}
throw new Error('Expected promise to be rejected')
} Since we didn't use edit: realised the underlying is Jest's incorrect handling of async tests — this workaround doesn't use them; hopefully it's still a useful workaround until this is resolved. |
(node:8040) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre |
Hope this will help you to understand the scene. https://spion.github.io/posts/why-i-am-switching-to-promises.html |
Wouldn't you want a UnhandledPromiseRejectionWarning to be treated as a test failure? In a previous project using Mocha, I set up make-promises-safe to fail the build when promise rejections weren't being caught. Unfortunately I haven't yet figured out how to get that working with Jest, likely because Jest has its own handler for promise rejections. Personally I dislike seeing warnings like this with no line numbers to go resolve the issues -- and because it doesn't fail the build, a bug could have been introduced long ago without noticing. Is there any way to make this better?
|
Ouch, this is forcing me to put return Promise.reject(...); in my application code... not sure what to do if you try to debug this: it(`should not produce unhandled Errors`, async () => {
async function rejected(throwMethod: 'reject' | 'throw' | null) {
try {
if (throwMethod === 'reject') {
return Promise.reject(Error('error from Promise.reject'));
}
if (throwMethod === 'throw') {
throw Error('error from throw');
}
return await fetch('hello');
} catch (e) {
throw e;
}
}
try {
await rejected('reject');
} catch (error) {
console.log(error);
expect(error).toHaveProperty('message', 'error from Promise.reject');
}
try {
await rejected('throw');
} catch (error) {
console.log(error);
expect(error).toHaveProperty('message', 'error from throw');
}
}); you get an unhandled error that's coming from (async () => {
async function rejected(throwMethod) {
try {
if (throwMethod === 'reject') {
return Promise.reject(Error('error from Promise.reject'));
}
if (throwMethod === 'throw') {
throw Error('error from throw');
}
return await fetch('hello');
} catch (e) {
throw e;
}
}
try {
await rejected('reject');
} catch (error) {
console.log(error);
}
try {
await rejected('throw');
} catch (error) {
console.log(error);
}
})() you get a console log of the two error, with no Unhandled |
jest 24.1.0, similar deprecation warning in unit test
|
The same to version 24.4.0 |
What does your test look like? Do you |
If your test looks like OP it's expected to still behave that way - we haven't done anything to fix it |
Came across this too, wasn't able to work around it so having to abandon Jest for now :( |
There's an article on Medium covering those node warnings: https://medium.com/dailyjs/how-to-prevent-your-node-js-process-from-crashing-5d40247b8ab2 Just made those changes on my main fetch and those warnings were gone from tests
|
any update on this? |
I have the same problem. |
…noring them See jestjs/jest#5311 (comment) ✗ Errored » Asynchronous Error in Integration tests in test/integration-test.js /home/macbre/github/phantomas/test/integration-test.js:19 throw err; ^ Error: Protocol error (Performance.getMetrics): Session closed. Most likely the page has been closed. at CDPSession.send (/home/macbre/github/phantomas/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:195:35) at Page.metrics (/home/macbre/github/phantomas/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:699:45) at /home/macbre/github/phantomas/lib/browser.js:266:37 at runMicrotasks (<anonymous>) at processTicksAndRejections (internal/process/task_queues.js:97:5)
Solution found here:
The idea is that when you |
This is a best answer! |
Hey peeps, now i'm getting crashes on my test instead of warning even after wrapping my async function in |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This might be the same issue as #3251 but I'm not sure so posting it separately.
With Jest 22.0.6, no config, this test:
(taken from an unrelated CRA issue)
produces this log:
Note these two lines:
The first line is being discussed in #3251 with a feature request of making the logging configurable. (There is also a separate report in #3251 (comment) of
catch()
failing to attach error handlers when it’s too late.)But I’m worried about the second line. Does it mean that when Node changes behavior as it advertises in the deprecation warning, all tests that print this message will start crashing the process? Or can I safely ignore the warning without worrying that my test suite will be very painful to upgrade to future Node versions when the behavior is flipped? Is there anything Jest should be doing to safeguard my test suite from crashing the test runner in the future, and is this a valid concern at all?
I'm using Node 8.9.1 on macOS Sierra.
The text was updated successfully, but these errors were encountered: