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
[jest] add jest.Spied*
utility types
#62781
[jest] add jest.Spied*
utility types
#62781
Conversation
@mrazauskas Thank you for submitting this PR! This is a live comment which I will keep updated. 1 package in this PR
@mrazauskas: I see that you have added yourself as an owner, are you sure you want to become an owner? Code ReviewsBecause this is a widely-used package, a DT maintainer will need to review it before it can be merged. You can test the changes of this PR in the Playground. Status
All of the items on the list are green. To merge, you need to post a comment including the string "Ready to merge" to bring in your changes. Diagnostic Information: What the bot saw about this PR{
"type": "info",
"now": "-",
"pr_number": 62781,
"author": "mrazauskas",
"headCommitOid": "48c7862b140c4c2c5ae433506f0ff9bd28842004",
"mergeBaseOid": "cf0f142bea53f5e1b5af343f3707395c52192a8b",
"lastPushDate": "2022-10-18T18:23:55.000Z",
"lastActivityDate": "2022-11-04T14:51:30.000Z",
"mergeOfferDate": "2022-11-04T14:51:09.000Z",
"mergeRequestDate": "2022-11-04T14:51:30.000Z",
"mergeRequestUser": "mrazauskas",
"hasMergeConflict": false,
"isFirstContribution": false,
"tooManyFiles": false,
"hugeChange": false,
"popularityLevel": "Critical",
"pkgInfo": [
{
"name": "jest",
"kind": "edit",
"files": [
{
"path": "types/jest/index.d.ts",
"kind": "definition"
},
{
"path": "types/jest/jest-tests.ts",
"kind": "test"
}
],
"owners": [
"NoHomey",
"jwbay",
"asvetliakov",
"alexjoverm",
"epicallan",
"ikatyang",
"wsmd",
"JamieMason",
"douglasduteil",
"ahnpnl",
"UselessPickles",
"r3nya",
"hotell",
"sebald",
"andys8",
"antoinebrault",
"gstamac",
"ExE-Boss",
"quassnoi",
"Belco90",
"tonyhallett",
"ycmjason",
"pawfa",
"gerkindev",
"domdomegg"
],
"addedOwners": [
"mrazauskas"
],
"deletedOwners": [],
"popularityLevel": "Critical"
}
],
"reviews": [
{
"type": "approved",
"reviewer": "sandersn",
"date": "2022-11-04T14:50:21.000Z",
"isMaintainer": true
},
{
"type": "approved",
"reviewer": "domdomegg",
"date": "2022-11-04T00:02:54.000Z",
"isMaintainer": false
}
],
"mainBotCommentID": 1282828299,
"ciResult": "pass"
} |
🔔 @NoHomey @jwbay @asvetliakov @alexjoverm @epicallan @ikatyang @wsmd @JamieMason @douglasduteil @ahnpnl @UselessPickles @r3nya @Hotell @sebald @andys8 @antoinebrault @gstamac @ExE-Boss @quassnoi @Belco90 @tonyhallett @ycmjason @pawfa @GerkinDev @domdomegg — please review this PR in the next few days. Be sure to explicitly select |
@@ -26,6 +26,7 @@ | |||
// Pawel Fajfer <https://github.com/pawfa> | |||
// Alexandre Germain <https://github.com/gerkindev> | |||
// Adam Jones <https://github.com/domdomegg> | |||
// Tom Mrazauskas <https://github.com/mrazauskas> |
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.
It would be nice to be notified if types are changing here, so I could adjust types in Jest repo.
Re-ping @NoHomey, @jwbay, @asvetliakov, @alexjoverm, @epicallan, @ikatyang, @wsmd, @JamieMason, @douglasduteil, @ahnpnl, @UselessPickles, @r3nya, @Hotell, @sebald, @andys8, @antoinebrault, @gstamac, @ExE-Boss, @quassnoi, @Belco90, @tonyhallett, @ycmjason, @pawfa, @GerkinDev: This PR has been out for over a week, yet I haven't seen any reviews. Could someone please give it some attention? Thanks! |
Ready to merge |
function spyOn<T extends {}, M extends ConstructorPropertyNames<Required<T>>>( | ||
object: T, | ||
method: M, | ||
): Required<T>[M] extends new (...args: any[]) => any | ||
? SpyInstance<InstanceType<Required<T>[M]>, ConstructorArgsType<Required<T>[M]>> | ||
: never; | ||
function spyOn<T extends {}, M extends FunctionPropertyNames<Required<T>>>( |
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.
Swapping the order here breaks this use case:
jest.spyOn(Storage.prototype, 'getItem').mockImplementation((key) => null);
ERROR: 570:42 expect TypeScript@5.0 compile error:
Property 'mockImplementation' does not exist on type 'never'.
undoing this change breaks
let typedSpy1: jest.SpiedClass<typeof globalThis.Date>;
typedSpy1 = jest.spyOn(globalThis, 'Date');
How can we make both work?
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.
See #63182. Seems like it will make both cases work as expected.
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.
Opened #63196 to address this.
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.
Missed that one, thanks!
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.
Just to add. As you noticed swapping the order made spyOn
work correctly with Date
. The type of Date
is rather odd indeed, but the same could be said about Storage
. Not so easy to cover both.
Please fill in this template.
npm test <package to test>
.If changing an existing definition:
jest.Spied*
utility types jestjs/jest#13440Summary
Jest v29.2.1 just got released with
jest.Spied*
utility types, which were added in jestjs/jest#13440. I adapted typings from this PR and added tests.