-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat(jest-mock): add mockFn.mock.lastCall
to retrieve last argument
#12285
Conversation
Codecov Report
@@ Coverage Diff @@
## main #12285 +/- ##
=======================================
Coverage 67.52% 67.52%
=======================================
Files 328 328
Lines 17244 17246 +2
Branches 5069 5070 +1
=======================================
+ Hits 11644 11646 +2
Misses 5567 5567
Partials 33 33
Continue to review full report at Codecov.
|
hey @SimenB does this sound useful to you? |
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 like it, thanks! Left some nits 🙂
@@ -79,6 +79,16 @@ mockFn.mock.instances[0] === a; // true | |||
mockFn.mock.instances[1] === b; // true | |||
``` | |||
|
|||
### `mockFn.mock.lastCall` |
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 should not be added to all versioned docs, only the ones in docs/
packages/jest-mock/src/index.ts
Outdated
@@ -536,6 +543,7 @@ export class ModuleMocker { | |||
calls: [], | |||
instances: [], | |||
invocationCallOrder: [], | |||
lastCall: undefined, |
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 should just not be here I think, not set to undefined
(which might be a return value)
packages/jest-mock/src/index.ts
Outdated
@@ -516,6 +520,9 @@ export class ModuleMocker { | |||
state = this._defaultMockState(); | |||
this._mockState.set(f, state); | |||
} | |||
|
|||
state.lastCall = state.calls[state.calls.length - 1]; |
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.
check if state.calls.length > 0
first
thanks @SimenB , will surely update this with the required changes |
Co-authored-by: Simen Bekkhus <[email protected]>
Co-authored-by: Simen Bekkhus <[email protected]>
Hm.. Jest has If there is a need users can add more custom matchers, hence keeping the I think the proposal in this PR is overlapping with current functionality (the Just wanted to leave a note. Might be I miss some use case. |
My main motivation is that it'd make |
yes that is the intention that i also had while introducing this one |
You know it better (; As I said, might be I miss some use cases. |
hey @SimenB is there any problem with my PR'S i am not getting any feedback , or am i doing something wrong? |
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.
Thanks!
hey @SimenB is there any problem with my PR'S i am not getting any feedback , or am i doing something wrong?
Nope, my energy just goes in bursts 🙂
mockFn.mock.lastCall
to retrieve last argument
I understand , sorry for my pings, take care. |
No worries! |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
When switching from using sinon mocks to jest, one of the nice things that sinon provided was the ability to retrieve the last call arguments easily. I know that you can use mockFn.mock.calls[mockFn.mock.calls.length - 1] to retrieve it, but mock.lastCall seems a lot easier when you're doing it in a bunch of places. I wanted to at least try adding it and see what the response was. I understand if this is not needed, but it seems like a nice little improvement.
Test plan
I've updated types, added tests for the feature and updated documentation. I ran yarn test for the entire test suite to verify that it was working.