From e7fc6ad9a74b271b8843fc5c6528700daa4b98ea Mon Sep 17 00:00:00 2001 From: mrazauskas <72159681+mrazauskas@users.noreply.github.com> Date: Tue, 5 Oct 2021 19:27:33 +0300 Subject: [PATCH] fix(expect): `toThrow` and `toThrowError` typings --- CHANGELOG.md | 1 + packages/expect/src/types.ts | 8 ++------ test-types/top-level-globals.test.ts | 6 ++++++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6fc46417d28..d2ff2963d008 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - `[expect]` Pass matcher context to asymmetric matchers ([#11926](https://github.com/facebook/jest/pull/11926) & [#11930](https://github.com/facebook/jest/pull/11930)) - `[expect]` Improve TypeScript types ([#11931](https://github.com/facebook/jest/pull/11931)) +- `[expect]` Improve typings of `toThrow()` and `toThrowError()` matchers ([#11929](https://github.com/facebook/jest/pull/11929)) - `[@jest/types]` Mark deprecated configuration options as `@deprecated` ([#11913](https://github.com/facebook/jest/pull/11913)) - `[jest-cli]` Improve `--help` printout by removing defunct `--browser` option ([#11914](https://github.com/facebook/jest/pull/11914)) - `[jest-haste-map]` Use distinct cache paths for different values of `computeDependencies` ([#11916](https://github.com/facebook/jest/pull/11916)) diff --git a/packages/expect/src/types.ts b/packages/expect/src/types.ts index 7642ebdccd62..691941f87b18 100644 --- a/packages/expect/src/types.ts +++ b/packages/expect/src/types.ts @@ -106,10 +106,6 @@ export type Expect = { not: InverseAsymmetricMatchers & ExtraAsymmetricMatchers; }; -interface Constructable { - new (...args: Array): unknown; -} - // This is a copy from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/de6730f4463cba69904698035fafd906a72b9664/types/jest/index.d.ts#L570-L817 export interface Matchers { /** @@ -322,11 +318,11 @@ export interface Matchers { /** * Used to test that a function throws when it is called. */ - toThrow(error?: string | Constructable | RegExp | Error): R; + toThrow(error?: unknown): R; /** * If you want to test that a specific error is thrown inside a function. */ - toThrowError(error?: string | Constructable | RegExp | Error): R; + toThrowError(error?: unknown): R; /* TODO: START snapshot matchers are not from `expect`, the types should not be here */ /** diff --git a/test-types/top-level-globals.test.ts b/test-types/top-level-globals.test.ts index 320aa875cfbb..544cba2bef5c 100644 --- a/test-types/top-level-globals.test.ts +++ b/test-types/top-level-globals.test.ts @@ -115,6 +115,12 @@ expectType(describe.skip.each(testTable)(testName, fn, timeout)); expectType(expect(2).toBe(2)); expectType>(expect(2).resolves.toBe(2)); +expectType(expect(() => {}).toThrow()); +expectType(expect(() => {}).toThrow(/error/)); +expectType(expect(() => {}).toThrow('error')); +expectType(expect(() => {}).toThrow(Error)); +expectType(expect(() => {}).toThrow(new Error('error'))); + expectType(expect('Hello').toEqual(expect.any(String))); // this currently does not error due to `[id: string]` in ExtraAsymmetricMatchers - we should have nothing there and force people to use interface merging