ant to test the following module index.ts
async function theFunctionFail() {
return await fail();
}
async function theFunctionSucceed() {
return await succeed();
}
async function fail() {
throw new Error();
}
async function succeed() {
return "a";
}
export { theFunctionFail, theFunctionSucceed };
using a test index.test.ts
import { theFunctionFail, theFunctionSucceed } from "../index";
it('theFunctionFail', () => {
expect(theFunctionFail()).rejects;
});
What does the UnhandledPromiseRejectionWarning
in the output
(node:10515) UnhandledPromiseRejectionWarning: Error
(node:10515) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:10515) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PASS src/__tests__/index.test.ts
✓ theFunctionFail (6ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.806s
Ran all test suites.
refer to? Wrapping expect(theFunctionFail()).rejects
in a try-catch(err)
block doesn't avoid the warning which I consider worth fixing.
Why don't the test fail?/How can I make the tests fail? If my test discovered a severe flaw, it shouldn't succeed in my understanding.
I'm using Typescript and Jest 24.7.1 with react-scripts
.
The unhandledrejection event is sent to the global scope of a script when a JavaScript Promise that has no rejection handler is rejected; typically, this is the window , but may also be a Worker . This is useful for debugging and for providing fallback error handling for unexpected situations.
Jest Basics: Describe Blocks. A describe block is used for organizing test cases in logical groups of tests. For example, we want to group all the tests for a specific class. We can further nest new describe blocks in an existing describe block.
This warning refers to the fact that an error is happening in the test that isn't being handled. The real problem is that your test isn't testing anything - there's an expect
without a matcher. Here's what it should be:
return expect(theFunctionFail()).rejects.toEqual(new Error());
See the Jest docs for .rejects
: https://jestjs.io/docs/en/tutorial-async#rejects
Note: It's also possible to use try/catch
, but you have to either await
like this:
it('theFunctionFail', async () => {
expect.assertions(1);
try {
await theFunctionFail();
} catch (err) {
expect(err).toEqual(new Error());
}
});
Or return the async function and catch the error (make sure you return the function):
it('theFunctionFail', () => {
expect.assertions(1);
return theFunctionFail().catch(err => {
expect(err).toEqual(new Error());
});
});
expect.assertions(number)
is a good way to make sure all of your expect
s are being called when testing asynchronous behavior.
Also, if you add an error message, e.g. new Error('oh no!')
, you will know for sure that you're catching the right error, and will make it a bit easier to debug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With