Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expect, @jest/expect): support type inference for toBe assertions #13470

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

- `[expect, @jest/expect]` support type inference for `toBe` assertions ([#13470](https://github.com/facebook/jest/pull/13470))
- `[@jest/globals, jest-mock]` Add `jest.Spied*` utility types ([#13440](https://github.com/facebook/jest/pull/13440))

### Fixes
Expand Down
13 changes: 7 additions & 6 deletions packages/expect/src/types.ts
Expand Up @@ -94,9 +94,9 @@ export interface BaseExpect {
}

export type Expect = {
<T = unknown>(actual: T): Matchers<void> &
<T = unknown>(actual: T): Matchers<void, T> &
Inverse<Matchers<void>> &
PromiseMatchers;
PromiseMatchers<T>;
} & BaseExpect &
AsymmetricMatchers &
Inverse<Omit<AsymmetricMatchers, 'any' | 'anything'>>;
Expand All @@ -118,7 +118,7 @@ export interface AsymmetricMatchers {
stringMatching(sample: string | RegExp): AsymmetricMatcher;
}

type PromiseMatchers = {
type PromiseMatchers<T = unknown> = {
/**
* Unwraps the reason of a rejected promise so any other matcher can be chained.
* If the promise is fulfilled the assertion fails.
Expand All @@ -128,10 +128,11 @@ type PromiseMatchers = {
* Unwraps the value of a fulfilled promise so any other matcher can be chained.
* If the promise is rejected the assertion fails.
*/
resolves: Matchers<Promise<void>> & Inverse<Matchers<Promise<void>>>;
resolves: Matchers<Promise<void>, Awaited<T>> &
Inverse<Matchers<Promise<void>>>;
};

export interface Matchers<R extends void | Promise<void>> {
export interface Matchers<R extends void | Promise<void>, T = unknown> {
/**
* Ensures the last call to a mock function was provided specific args.
*/
Expand All @@ -152,7 +153,7 @@ export interface Matchers<R extends void | Promise<void>> {
* Checks that a value is what you expect. It calls `Object.is` to compare values.
* Don't use `toBe` with floating-point numbers.
*/
toBe(expected: unknown): R;
toBe(expected: T): R;
/**
* Ensures that a mock function is called.
*/
Expand Down
12 changes: 7 additions & 5 deletions packages/jest-expect/src/types.ts
Expand Up @@ -29,22 +29,24 @@ type Inverse<Matchers> = {
not: Matchers;
};

type JestMatchers<R extends void | Promise<void>, T> = Matchers<R> &
type JestMatchers<R extends void | Promise<void>, T = unknown> = Matchers<
R,
T
> &
SnapshotMatchers<R, T>;

type PromiseMatchers<T = unknown> = {
/**
* Unwraps the reason of a rejected promise so any other matcher can be chained.
* If the promise is fulfilled the assertion fails.
*/
rejects: JestMatchers<Promise<void>, T> &
Inverse<JestMatchers<Promise<void>, T>>;
rejects: JestMatchers<Promise<void>> & Inverse<JestMatchers<Promise<void>>>;
/**
* Unwraps the value of a fulfilled promise so any other matcher can be chained.
* If the promise is rejected the assertion fails.
*/
resolves: JestMatchers<Promise<void>, T> &
Inverse<JestMatchers<Promise<void>, T>>;
resolves: JestMatchers<Promise<void>, Awaited<T>> &
Inverse<JestMatchers<Promise<void>>>;
};

declare module 'expect' {
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-types/__typetests__/expect.test.ts
Expand Up @@ -221,6 +221,11 @@ expectType<void>(
),
);

expectType<void>(
expect({age: 12, name: 'someName'}).toBe({age: 13, name: 'someOtherName'}),
);
expectError(expect({age: 12, name: 'someName'}).toBe({name: 'someOtherName'}));

expectType<void>(expect(jest.fn()).toHaveBeenCalledWith());
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123));
expectType<void>(expect(jest.fn()).toHaveBeenCalledWith(123, 'value'));
Expand Down