In order to test that an exception gets thrown, Jest uses the following syntax:
it("throws and exception", () => {
expect(() => {
throw new Error("My Error");
}).toThrow(new Error("My Error");
});
Do be aware, when asserting the error message, empty strings are ignored, so the following are equivalent:
.toThrow();
.toThrow("");
.toThrow(new Error(""));
Adapted from: