-
-
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
async/ await toThrow is not working #1700
Comments
Yes, this isn't supported currently. See #1377 |
Instead of expect(async () => await check()).toThrow()
// Jest error: "Expected the function to throw an error. But it didn't throw anything." this works: expect(check()).rejects.toEqual(new Error('Test')) Yeah the syntax is awkward... |
You can also write: let error;
try {
await check();
} catch (e) {
error = e;
}
expect(error).toEqual(new Error('Test')); |
The following worked for me as well: async function check() {
throw new Error('Test');
}
expect(check()).rejects.toEqual(new Error('Test')) See: https://facebook.github.io/jest/docs/en/tutorial-async.html#rejects |
Adding to the list of workarounds. async function check() {
throw new SomeSpecificError('Whatever');
}
await check()
.then(() => {
throw new Error('Should go to .catch, not enter .then');
})
.catch((err) => {
expect(err).toBeInstanceOf(SomeSpecificError);
}); |
await expect(check).rejects.toThrow(SomeSpecificError); Should work as well |
await expect(check).rejects.toThrow(/DUPLICATES_DETECTED/); is what I do. |
async function check() {
throw new Error("Test");
}
await expect(check).rejects.toThrow(Error); expect(received).rejects.toThrow()
received value must be a Promise.
Received:
function: [Function check] |
You need to invoke it to give it the actual promise |
async function check() {
throw new Error("Test");
}
await expect(check()).rejects.toThrow(Error); Should work as well it(`some test`, async () => {
async function check() {
try {
return Promise.reject(await asyncMethod());
} catch (error) {
throw new Error("test");
}
}
await expect(check()).rejects.toThrow(Error);
}); can work version
|
I know it need a promise, but the case you're offering is not :) |
Hi ! I might be doing something wrong, but I still have an issue with custom errors in async calls. Consider the following: class CustomErrorType {}
// ...
test('check throws custom error', async () => {
async function check() {
throw new CustomErrorType();
}
await expect(check()).rejects.toThrow(CustomErrorType);
}); then the test fails with the following output:
So the test fails - whilst it works perfectly when the thrown class is class CustomErrorType extends Error {} then the error is
Any clue on something what is wrong in that sample ? I'm using jest 23.4.2. |
@Marchelune When you write |
In case someone throw anything different from instance Error class and struggle to use advise from that thread (as I did).
but this will work (not sure if there is a better way):
|
@Karabur typo |
Every time I did `await expect(...).rejects.toThrow(...)` jest would throw. Error here: https://travis-ci.org/artf/grapesjs/jobs/600715489#L1147 Many comments in the jest github issues (jestjs/jest#1700) say that this syntax should work but I don't believe it does.
Here's an explicit test that assures it will definitely break if it does NOT throw and matches specific error. yourCoolAsyncMethod('yeah it is')
.then(ok => { // Presumes not a Promise<void>
expect(ok).toBeUndefined();
done();
})
.catch(bad => {
expect(bad).toBeDefined();
expect(bad).toMatchInlineSnapshot(
`Specifically serious error!`
);
done();
}); |
Hi @SimenB.
InternalServerErrorException is thrown with an object.... How can I also assert for properties on the error thrown? For example:
|
Problem about this is that your test will pass if the code never has errors. So this test would not really offer much value as it's expected results are varied based on whether the code it's testing has an error or not. Tests should have a negative flow or a positive flow and we should only test what is needed to test. This sort of randomness isn't the greatest. |
Hey @David-Tennant 🖐 Thanks for pointing out that. I was working on a Thanks 💨 |
Howdy @futuredayv 👋
The problem is that your test will still pass if the test does return void. Which should be a bug right? yourCoolAsyncMethod('yeah it is')
// -- Lets say your method does not throw, and returns void instead
.then(ok => { // Presumes not a Promise<void>
expect(ok).toBeUndefined(); // -- At this point if it was void, this would pass
done(); // -- Then your test would end and the catch would not be reached
// -- Now you have unknown behavior passing a test where it's suppose to catch a fail
})
.catch(bad => {
// -- This block does not run because done() is called above
expect(bad).toBeDefined();
expect(bad).toMatchInlineSnapshot(
`Specifically serious error!`
);
done();
}); Not sure what the solution would be for your use case. But I would start by not calling yourCoolAsyncMethod('yeah it is')
.then(ok => throw new Error("This block of code should not be reached")
.catch(bad => {
expect(bad).toBeDefined();
expect(bad).toMatchInlineSnapshot(
`Specifically serious error!`
);
done();
}); |
A slightly better way is to use
|
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. |
next test
will fail and don't catch error properly (I am using latest version of jest)
The text was updated successfully, but these errors were encountered: