toThrowErrorAsync
Runs a function and asserts that it throws an error or results in a promise that’s rejected.
toThrowErrorAsync(...args: any[]): Promise<Assertion<any>>
Note
This function is asynchronous and must beawait
-ed.
Parameters
- args — The arguments passed directly to the function, if any
Return value
A promise for a new assertion for the error that’s caught. The negated version NegatedAssertion.toThrowErrorAsync() (i.e. .not.toThrowErrorAsync()
) returns a promise for an assertion for the function’s return value instead.
Example
// catch an error or rejected promise:
await expect(async () => {
throw Error("Catch me");
}).toThrowErrorAsync();
// check the error message:
let expectError = await expect(async () => {
throw Error("Catch me");
}).toThrowErrorAsync();
expectError
.toHaveProperty("message")
.toMatchRegExp(/Catch/);
Related
- class AssertionRepresents a value ready to be asserted.
- toThrowError(…args)Runs a function and asserts that it throws an error when called with given arguments.