Skip to content

Commit e420155

Browse files
authored
fix(expect): toThrow and toThrowError typings (#11929)
1 parent 508827c commit e420155

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `[expect]` Pass matcher context to asymmetric matchers ([#11926](https://github.com/facebook/jest/pull/11926) & [#11930](https://github.com/facebook/jest/pull/11930))
88
- `[expect]` Improve TypeScript types ([#11931](https://github.com/facebook/jest/pull/11931))
9+
- `[expect]` Improve typings of `toThrow()` and `toThrowError()` matchers ([#11929](https://github.com/facebook/jest/pull/11929))
910
- `[@jest/types]` Mark deprecated configuration options as `@deprecated` ([#11913](https://github.com/facebook/jest/pull/11913))
1011
- `[jest-cli]` Improve `--help` printout by removing defunct `--browser` option ([#11914](https://github.com/facebook/jest/pull/11914))
1112
- `[jest-haste-map]` Use distinct cache paths for different values of `computeDependencies` ([#11916](https://github.com/facebook/jest/pull/11916))

packages/expect/src/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ export type Expect<State extends MatcherState = MatcherState> = {
106106
not: InverseAsymmetricMatchers & ExtraAsymmetricMatchers;
107107
};
108108

109-
interface Constructable {
110-
new (...args: Array<unknown>): unknown;
111-
}
112-
113109
// This is a copy from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/de6730f4463cba69904698035fafd906a72b9664/types/jest/index.d.ts#L570-L817
114110
export interface Matchers<R> {
115111
/**
@@ -322,11 +318,11 @@ export interface Matchers<R> {
322318
/**
323319
* Used to test that a function throws when it is called.
324320
*/
325-
toThrow(error?: string | Constructable | RegExp | Error): R;
321+
toThrow(error?: unknown): R;
326322
/**
327323
* If you want to test that a specific error is thrown inside a function.
328324
*/
329-
toThrowError(error?: string | Constructable | RegExp | Error): R;
325+
toThrowError(error?: unknown): R;
330326

331327
/* TODO: START snapshot matchers are not from `expect`, the types should not be here */
332328
/**

test-types/top-level-globals.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ expectType<void>(describe.skip.each(testTable)(testName, fn, timeout));
115115
expectType<void>(expect(2).toBe(2));
116116
expectType<Promise<void>>(expect(2).resolves.toBe(2));
117117

118+
expectType<void>(expect(() => {}).toThrow());
119+
expectType<void>(expect(() => {}).toThrow(/error/));
120+
expectType<void>(expect(() => {}).toThrow('error'));
121+
expectType<void>(expect(() => {}).toThrow(Error));
122+
expectType<void>(expect(() => {}).toThrow(new Error('error')));
123+
118124
expectType<void>(expect('Hello').toEqual(expect.any(String)));
119125

120126
// this currently does not error due to `[id: string]` in ExtraAsymmetricMatchers - we should have nothing there and force people to use interface merging

0 commit comments

Comments
 (0)