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(@jest/expect): Expose type of actual to Matchers #13848

Merged
merged 8 commits into from Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[@jest/expect]` provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions (#13848)
benjaminjkraft marked this conversation as resolved.
Show resolved Hide resolved

### Chore & Maintenance

- `[*]` make sure to exclude `.eslintcache` from published module ([#13832](https://github.com/facebook/jest/pull/13832))
Expand Down
1 change: 1 addition & 0 deletions packages/expect/__typetests__/expect.test.ts
Expand Up @@ -19,6 +19,7 @@ import {
import type * as jestMatcherUtils from 'jest-matcher-utils';

type M = Matchers<void>;
type N = Matchers<void, string>;

expectError(() => {
type E = Matchers;
Expand Down
22 changes: 22 additions & 0 deletions packages/expect/__typetests__/expectTyped.test.ts
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {expectAssignable, expectError, expectType} from 'tsd-lite';
import {Matchers, expect} from 'expect';

declare module 'expect' {
interface Matchers<R, T> {
toTypedEqual(expected: T): void;
}
}

expectType<void>(expect(100).toTypedEqual(100));
expectType<void>(expect(101).not.toTypedEqual(101));

expectError(() => {
expect(100).toTypedEqual('three');
});
16 changes: 9 additions & 7 deletions packages/expect/src/types.ts
Expand Up @@ -97,9 +97,9 @@ export interface BaseExpect {
}

export type Expect = {
<T = unknown>(actual: T): Matchers<void> &
Inverse<Matchers<void>> &
PromiseMatchers;
<T = unknown>(actual: T): Matchers<void, T> &
Inverse<Matchers<void, T>> &
PromiseMatchers<T>;
} & BaseExpect &
AsymmetricMatchers &
Inverse<Omit<AsymmetricMatchers, 'any' | 'anything'>>;
Expand All @@ -121,20 +121,22 @@ 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.
*/
rejects: Matchers<Promise<void>> & Inverse<Matchers<Promise<void>>>;
rejects: Matchers<Promise<void>, T> & Inverse<Matchers<Promise<void>, T>>;
/**
* 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>, T> & Inverse<Matchers<Promise<void>, T>>;
};

export interface Matchers<R extends void | Promise<void>> {
// @ts-expect-error unused variable T (can't use _T since users redeclare Matchers)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface Matchers<R extends void | Promise<void>, T = unknown> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this:

Suggested change
// @ts-expect-error unused variable T (can't use _T since users redeclare Matchers)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface Matchers<R extends void | Promise<void>, T = unknown> {
export interface Matchers<R extends void | Promise<void>, T = unknown> {
/** @internal */
_doKeepT(expect: T): R;

Copy link
Contributor

@mrazauskas mrazauskas Feb 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have some longer comment, but seems to be working. Or?

stripInternal documentation: https://www.typescriptlang.org/tsconfig#stripInternal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, great! I thought of adding a fake method but didn't realize I could hide it like that.

/**
* Ensures the last call to a mock function was provided specific args.
*/
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-expect/__typetests__/jest-expect.test.ts
Expand Up @@ -29,3 +29,16 @@ expectError(() => {

expectAssignable<typeof expect>(jestExpect);
expectNotAssignable<typeof jestExpect>(expect);

declare module 'expect' {
interface Matchers<R, T> {
toTypedEqual(expected: T): void;
}
}

expectType<void>(jestExpect(100).toTypedEqual(100));
expectType<void>(jestExpect(101).not.toTypedEqual(101));

expectError(() => {
jestExpect(100).toTypedEqual('three');
});
2 changes: 1 addition & 1 deletion packages/jest-expect/src/types.ts
Expand Up @@ -29,7 +29,7 @@ type Inverse<Matchers> = {
not: Matchers;
};

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

type PromiseMatchers<T = unknown> = {
Expand Down