method
test.Matchers.toThrowError
expected?: unknown
): void;
Asserts that a function throws an error.
- If expected is a
stringorRegExp, it checks themessageproperty. - If expected is an
Errorobject, it checks thenameandmessageproperties. - If expected is an
Errorconstructor, it checks the class of theError. - If expected is not provided, it checks that anything was thrown.
@param expected
the expected error, error message, or error pattern
function fail() {
throw new Error("Oops!");
}
expect(fail).toThrowError("Oops!");
expect(fail).toThrowError(/oops/i);
expect(fail).toThrowError(Error);
expect(fail).toThrowError();