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
- class AssertionRepresents a value ready to be asserted.
- toThrowErrorAsync(…args)Runs a function and asserts that it throws an error or results in a promise that’s rejected.