Docs (4.0.0)
class Assertion method

toThrowError

Runs a function and asserts that it throws an error when called with given arguments.

toThrowError(...args: any[]): Assertion<any>

Parameters

  • args — The arguments passed directly to the function, if any

Return value

A new assertion for the error that was caught. The negated version NegatedAssertion.toThrowError() (i.e. .not.toThrowError()) returns an assertion for the function’s return value instead.

Example

// catch an error:
expect(() => {
  throw Error("Catch me");
}).toThrowError();

// check the error message:
expect(() => {
  throw Error("Catch me");
})
   .toThrowError()
  .toHaveProperty("message")
  .toMatchRegExp(/Catch/);

Related