-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need these any
assertions here? I think those can be removed.
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.
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. |
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.
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 |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected?: any[] | |
expected?: unknown[] |
( matcherName: string, methodName: string ) => ( received: any ) => { | ||
const spy = received[ methodName ] as Mock; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function ( received: any, ...expected: any[] ) { | |
const spy = received[ methodName ] as Mock; | |
function ( received: Record< string, Mock >, ...expected: unknown[] ) { | |
const spy = received[ methodName ]; |
@@ -87,6 +101,6 @@ expect.extend( | |||
), | |||
}; | |||
}, | |||
{} | |||
{} as Record< string, any > |
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.
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 |
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.
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.
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.
Perfect. Thanks
Co-authored-by: kushagra-goyal-14 <kush123@git.wordpress.org> Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org>
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.