Skip to content

Commit

Permalink
fix(expect): toThrow and toThrowError typings (#11929)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Oct 5, 2021
1 parent 508827c commit e420155
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 2 additions & 6 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ export type Expect<State extends MatcherState = MatcherState> = {
not: InverseAsymmetricMatchers & ExtraAsymmetricMatchers;
};

interface Constructable {
new (...args: Array<unknown>): unknown;
}

// This is a copy from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/de6730f4463cba69904698035fafd906a72b9664/types/jest/index.d.ts#L570-L817
export interface Matchers<R> {
/**
Expand Down Expand Up @@ -322,11 +318,11 @@ export interface Matchers<R> {
/**
* 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 */
/**
Expand Down
6 changes: 6 additions & 0 deletions test-types/top-level-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ expectType<void>(describe.skip.each(testTable)(testName, fn, timeout));
expectType<void>(expect(2).toBe(2));
expectType<Promise<void>>(expect(2).resolves.toBe(2));

expectType<void>(expect(() => {}).toThrow());
expectType<void>(expect(() => {}).toThrow(/error/));
expectType<void>(expect(() => {}).toThrow('error'));
expectType<void>(expect(() => {}).toThrow(Error));
expectType<void>(expect(() => {}).toThrow(new Error('error')));

expectType<void>(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
Expand Down

0 comments on commit e420155

Please sign in to comment.