-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(@jest/expect): Expose type of actual to Matchers #13848
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
Changes from 3 commits
4bfbc6b
b9c5c27
d6459b4
2b7490e
2b4fa2d
fc53bed
6230a94
12d3bcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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'>>; | ||||||||||||||
|
|
@@ -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> { | ||||||||||||||
|
||||||||||||||
| // @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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.