Skip to content
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

Merged
merged 7 commits into from
Feb 5, 2022
Merged

feat(jest-mock): add mockFn.mock.lastCall to retrieve last argument #12285

merged 7 commits into from
Feb 5, 2022

Conversation

Biki-das
Copy link
Contributor

@Biki-das Biki-das commented Jan 31, 2022

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.

@codecov-commenter
Copy link

codecov-commenter commented Jan 31, 2022

Codecov Report

Merging #12285 (41a82d4) into main (4f4be57) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           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           
Impacted Files Coverage Δ
packages/jest-mock/src/index.ts 84.87% <100.00%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f4be57...41a82d4. Read the comment docs.

@Biki-das
Copy link
Contributor Author

Biki-das commented Feb 1, 2022

hey @SimenB does this sound useful to you?

Copy link
Member

@SimenB SimenB left a 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`
Copy link
Member

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/

@@ -536,6 +543,7 @@ export class ModuleMocker {
calls: [],
instances: [],
invocationCallOrder: [],
lastCall: undefined,
Copy link
Member

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)

@@ -516,6 +520,9 @@ export class ModuleMocker {
state = this._defaultMockState();
this._mockState.set(f, state);
}

state.lastCall = state.calls[state.calls.length - 1];
Copy link
Member

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

CHANGELOG.md Outdated Show resolved Hide resolved
@Biki-das
Copy link
Contributor Author

Biki-das commented Feb 2, 2022

thanks @SimenB , will surely update this with the required changes

@mrazauskas
Copy link
Contributor

Hm.. Jest has .lastCalledWith() and many other matchers (like .toBeCalled(), .toBeCalledTimes(), etc) to make mockFn related assertions. It seemed to me like a good design decision – slim .mock object with raw data and a set of matchers to make specific assertions based on that data. To quote the docs: "These matchers are sugar for common forms of inspecting the .mock property."

If there is a need users can add more custom matchers, hence keeping the .mock object generic makes sense. The point here is that extensibility is handled through matchers, not through getters on mockFn.mock (for instance, callCount could be another candidate).

I think the proposal in this PR is overlapping with current functionality (the .lastCalledWith() matcher) and does not follow current design (if I get it right).

Just wanted to leave a note. Might be I miss some use case.

@SimenB
Copy link
Member

SimenB commented Feb 2, 2022

My main motivation is that it'd make jest-mock more usable outside of expect

@Biki-das
Copy link
Contributor Author

Biki-das commented Feb 2, 2022

My main motivation is that it'd make jest-mock more usable outside of expect

yes that is the intention that i also had while introducing this one

@mrazauskas
Copy link
Contributor

My main motivation is that it'd make jest-mock more usable outside of expect

You know it better (; As I said, might be I miss some use cases.

@Biki-das Biki-das requested a review from SimenB February 2, 2022 09:31
@Biki-das
Copy link
Contributor Author

Biki-das commented Feb 4, 2022

hey @SimenB is there any problem with my PR'S i am not getting any feedback , or am i doing something wrong?

Copy link
Member

@SimenB SimenB left a 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 🙂

@SimenB SimenB changed the title Doc :- Added mockFn.mock.lastCall to retreive last Arguement feat(jest-mock): add mockFn.mock.lastCall to retrieve last argument Feb 5, 2022
@SimenB SimenB merged commit b2db6cf into jestjs:main Feb 5, 2022
@Biki-das
Copy link
Contributor Author

Biki-das commented Feb 5, 2022

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 🙂

I understand , sorry for my pings, take care.

@SimenB
Copy link
Member

SimenB commented Feb 5, 2022

No worries!

@github-actions
Copy link

github-actions bot commented Mar 8, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants