TypeScript: migrate jest-console package to TS - #70538
Conversation
- remove js test file - add spaces and fix docs
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| expect( ( spy as any ).assertionsNumber ).toBe( 0 ); | ||
|
|
||
| console[ methodName ]( message ); | ||
|
|
||
| expect( console )[ matcherName ](); | ||
| expect( spy.assertionsNumber ).toBe( 1 ); | ||
| expect( ( spy as any ).assertionsNumber ).toBe( 1 ); | ||
|
|
||
| expect( console )[ matcherNameWith ]( message ); | ||
| expect( spy.assertionsNumber ).toBe( 2 ); | ||
| expect( ( spy as any ).assertionsNumber ).toBe( 2 ); |
There was a problem hiding this comment.
Why do we need these any assertions here? I think those can be removed.
There was a problem hiding this comment.
Yes, I have removed those unnecessary types from the tests.
|
Hey @manzoorwanijk , Thank you for the review. I have updated the PR with the changes. Can you please re-review or ship the PR whenever possible. |
manzoorwanijk
left a comment
There was a problem hiding this comment.
I still see some as any assertions but I am not sure if we can entirely get rid of all of those.
Here, there are some cases with usage of |
manzoorwanijk
left a comment
There was a problem hiding this comment.
unknown is a safer type than any because it narrows the scope instead of expanding it.
So, let us use unknown where we don't really know the type.
We can also improve some any types with some properly defined types.
| calls: any[][]; | ||
| matcherName: string; | ||
| methodName: string; | ||
| expected?: any[]; |
There was a problem hiding this comment.
| calls: any[][]; | |
| matcherName: string; | |
| methodName: string; | |
| expected?: any[]; | |
| calls: unknown[][]; | |
| matcherName: string; | |
| methodName: string; | |
| expected?: unknown[]; |
| spy: Mock, | ||
| matcherName: string, | ||
| methodName: string, | ||
| expected?: any[] |
There was a problem hiding this comment.
| expected?: any[] | |
| expected?: unknown[] |
| ( matcherName: string, methodName: string ) => ( received: any ) => { | ||
| const spy = received[ methodName ] as Mock; |
There was a problem hiding this comment.
We can get rid of any type and the assertion
| ( matcherName: string, methodName: string ) => ( received: any ) => { | |
| const spy = received[ methodName ] as Mock; | |
| ( matcherName: string, methodName: string ) => | |
| ( received: Record< string, Mock > ) => { | |
| const spy = received[ methodName ]; |
| function ( received: any, ...expected: any[] ) { | ||
| const spy = received[ methodName ] as Mock; |
There was a problem hiding this comment.
| function ( received: any, ...expected: any[] ) { | |
| const spy = received[ methodName ] as Mock; | |
| function ( received: Record< string, Mock >, ...expected: unknown[] ) { | |
| const spy = received[ methodName ]; |
| }; | ||
| }, | ||
| {} | ||
| {} as Record< string, any > |
There was a problem hiding this comment.
Instead of this assertion here, I would pass this as the first argument to th .reduce generic
Record<
string,
( received: Record< string, Mock > ) => {
pass: boolean;
message: () => string;
}
>So, the .reduce call would look like this
Object.entries( supportedMatchers ).reduce<
Record<
string,
( received: Record< string, Mock > ) => {
pass: boolean;
message: () => string;
}
>
>(/* Arguments here */);|
I've implemented the suggested changes to improve type safety in the jest-console package. Created a dedicated cc: @manzoorwanijk |
manzoorwanijk
left a comment
There was a problem hiding this comment.
We also need to update the types field in package.json for the package to build-types but also handle the existing types/index.d.ts, which IMHO can be changed to declarations.d.ts and included via files field in tsconfig.json.
Co-authored-by: kushagra-goyal-14 <kush123@git.wordpress.org> Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
|
I wonder if this might have regressed the availability of typings for Jest matchers in downstream projects that use this package? In
With the latest version (
I suspect it may be related to the change here to move the Jest Compare types root between 8.26.0 and 8.28.0:
Edit: Trying to explore a fix at #71215 |


What?
Part of #67691
Migrating the jest-console package to TypeScript.
Why?
To enhance DX and add type safety.
How?
By porting the code and tests to TypeScript.
Testing Instructions
Typecheck and unit tests.