-
Notifications
You must be signed in to change notification settings - Fork 4.9k
TypeScript: migrate jest-console package to TS #70538
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 6 commits
5801b93
df93b19
0a0f51f
1141923
bf1b8f4
44aa270
5c627f2
7b00a26
28300d7
daebc36
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,8 +7,18 @@ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; | |||||||||||
| * Internal dependencies | ||||||||||||
| */ | ||||||||||||
| import supportedMatchers from './supported-matchers'; | ||||||||||||
| import type { Mock } from 'jest-mock'; | ||||||||||||
|
|
||||||||||||
| const createErrorMessage = ( spyInfo ) => { | ||||||||||||
| interface SpyInfo { | ||||||||||||
| spy: Mock; | ||||||||||||
| pass: boolean; | ||||||||||||
| calls: any[][]; | ||||||||||||
| matcherName: string; | ||||||||||||
| methodName: string; | ||||||||||||
| expected?: any[]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const createErrorMessage = ( spyInfo: SpyInfo ) => { | ||||||||||||
| const { spy, pass, calls, matcherName, methodName, expected } = spyInfo; | ||||||||||||
| const hint = pass ? `.not${ matcherName }` : matcherName; | ||||||||||||
| const message = pass | ||||||||||||
|
|
@@ -28,7 +38,12 @@ const createErrorMessage = ( spyInfo ) => { | |||||||||||
| 'See https://www.npmjs.com/package/@wordpress/jest-console for details.'; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| const createSpyInfo = ( spy, matcherName, methodName, expected ) => { | ||||||||||||
| const createSpyInfo = ( | ||||||||||||
| spy: Mock, | ||||||||||||
| matcherName: string, | ||||||||||||
| methodName: string, | ||||||||||||
| expected?: any[] | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| ) => { | ||||||||||||
| const calls = spy.mock.calls; | ||||||||||||
|
|
||||||||||||
| const pass = expected | ||||||||||||
|
|
@@ -51,22 +66,21 @@ const createSpyInfo = ( spy, matcherName, methodName, expected ) => { | |||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| const createToHaveBeenCalledMatcher = | ||||||||||||
| ( matcherName, methodName ) => ( received ) => { | ||||||||||||
| const spy = received[ methodName ]; | ||||||||||||
| ( matcherName: string, methodName: string ) => ( received: any ) => { | ||||||||||||
| const spy = received[ methodName ] as Mock; | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can get rid of
Suggested change
|
||||||||||||
| const spyInfo = createSpyInfo( spy, matcherName, methodName ); | ||||||||||||
|
|
||||||||||||
| spy.assertionsNumber += 1; | ||||||||||||
|
|
||||||||||||
| ( spy as any ).assertionsNumber += 1; | ||||||||||||
| return spyInfo; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| const createToHaveBeenCalledWith = ( matcherName, methodName ) => | ||||||||||||
| function ( received, ...expected ) { | ||||||||||||
| const spy = received[ methodName ]; | ||||||||||||
| const createToHaveBeenCalledWith = ( | ||||||||||||
| matcherName: string, | ||||||||||||
| methodName: string | ||||||||||||
| ) => | ||||||||||||
| function ( received: any, ...expected: any[] ) { | ||||||||||||
| const spy = received[ methodName ] as Mock; | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| const spyInfo = createSpyInfo( spy, matcherName, methodName, expected ); | ||||||||||||
|
|
||||||||||||
| spy.assertionsNumber += 1; | ||||||||||||
|
|
||||||||||||
| ( spy as any ).assertionsNumber += 1; | ||||||||||||
| return spyInfo; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
|
|
@@ -87,6 +101,6 @@ expect.extend( | |||||||||||
| ), | ||||||||||||
| }; | ||||||||||||
| }, | ||||||||||||
| {} | ||||||||||||
| {} as Record< string, any > | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of this assertion here, I would pass this as the first argument to th Record<
string,
( received: Record< string, Mock > ) => {
pass: boolean;
message: () => string;
}
>So, the Object.entries( supportedMatchers ).reduce<
Record<
string,
( received: Record< string, Mock > ) => {
pass: boolean;
message: () => string;
}
>
>(/* Arguments here */); |
||||||||||||
| ) | ||||||||||||
| ); | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/tsconfig.json", | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "types": [ "jest", "@testing-library/jest-dom" ] | ||
| } | ||
| } |
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.