Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  52] [ 7]  / answers: 1 / hits: 29328  / 5 Years ago, fri, may 24, 2019, 12:00:00

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.


More From » typescript

 Answers
16

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 expects 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.


[#52080] Thursday, May 16, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leannjaidynd

Total Points: 111
Total Questions: 100
Total Answers: 94

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
;