diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index d11f79df64..432ba6f36f 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -563,11 +563,11 @@ declare namespace jest { /** * Used to test that a function throws when it is called. */ - toThrow(error?: string | Constructable | RegExp): R; + toThrow(error?: string | Constructable | RegExp | Error): R; /** * If you want to test that a specific error is thrown inside a function. */ - toThrowError(error?: string | Constructable | RegExp): R; + toThrowError(error?: string | Constructable | RegExp | Error): R; /** * Used to test that a function throws a error matching the most recent snapshot when it is called. */ diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 7d10075262..84b2007854 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -556,15 +556,18 @@ describe("", () => { expect(true).toStrictEqual(false); expect({}).toStrictEqual({}); + const errInstance = new Error(); + const willThrow = () => { throw new Error(); }; expect(() => {}).toThrow(); - expect(() => { throw new Error(); }).toThrow(""); + expect(willThrow).toThrow(""); + expect(willThrow).toThrow(errInstance); expect(jest.fn()).toThrow(Error); - expect(jest.fn(() => { throw new Error(); })).toThrow(/foo/); + expect(jest.fn(willThrow)).toThrow(/foo/); expect(() => {}).toThrowErrorMatchingSnapshot(); - expect(() => { throw new Error(); }).toThrowErrorMatchingSnapshot(); + expect(willThrow).toThrowErrorMatchingSnapshot(); expect(jest.fn()).toThrowErrorMatchingSnapshot(); - expect(jest.fn(() => { throw new Error(); })).toThrowErrorMatchingSnapshot(); + expect(jest.fn(willThrow)).toThrowErrorMatchingSnapshot(); /* not */