Docs (4.0.0)
class Assertion method

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 be await-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