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

Fix bugs with mock/spy result tracking of recursive functions. #6381

Merged
merged 30 commits into from Oct 31, 2018
Merged

Fix bugs with mock/spy result tracking of recursive functions. #6381

merged 30 commits into from Oct 31, 2018

Conversation

UselessPickles
Copy link
Contributor

@UselessPickles UselessPickles commented Jun 2, 2018

Summary

Results of mocks/spies were not being recorded properly for recursive functions because the result of the call was pushed onto an array AFTER the function returned (results were in reverse order).

I have reworked the result recording code to push an object onto the "results" array immediately upon calling the mock (with values representing an "incomplete" call), then update that same result object upon returning.

  • Results are now recorded in the right order during recursion.
  • All the "return" expect matchers have been updated to be aware of incomplete calls.

Test plan

See new unit tests. Edge cases related to recursion are now covered (including testing mock result state and calling expect return matchers from inside of the recursive function).

Breaking Change

The structure of the MockFunctionResult has changed. The property isThrow: boolean has changed to type: 'return' | 'throw' | 'incomplete'.

@codecov-io
Copy link

codecov-io commented Jun 2, 2018

Codecov Report

Merging #6381 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6381      +/-   ##
==========================================
+ Coverage   63.57%   63.59%   +0.01%     
==========================================
  Files         235      235              
  Lines        8992     8996       +4     
  Branches        3        4       +1     
==========================================
+ Hits         5717     5721       +4     
  Misses       3274     3274              
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-mock/src/index.js 87.65% <100%> (+0.15%) ⬆️

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 6c61baa...57deef3. Read the comment docs.

@rickhanlonii
Copy link
Member

Great find! Appreciate all the contributions you're making to Jest @UselessPickles!

@UselessPickles
Copy link
Contributor Author

One of the CI tests failed during yarn install:

[2/4] Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.6.0.tgz: Parse Error".

Any ideas?

@SimenB
Copy link
Member

SimenB commented Jun 2, 2018

CI seems green?

@thymikee
Copy link
Collaborator

thymikee commented Jun 2, 2018

I've silently restarted the build :)

CHANGELOG.md Outdated Show resolved Hide resolved
@@ -96,7 +100,7 @@
- `[jest-diff]` Support returning diff from oneline strings ([#6221](https://github.com/facebook/jest/pull/6221))
- `[expect]` Improve return matchers ([#6172](https://github.com/facebook/jest/pull/6172))
- `[jest-cli]` Overhaul watch plugin hooks names ([#6249](https://github.com/facebook/jest/pull/6249))
- `[jest-mock]` Include tracked call results in serialized mock ([#6244](https://github.com/facebook/jest/pull/6244))
- `[jest-mock]` [**BREAKING**] Include tracked call results in serialized mock ([#6244](https://github.com/facebook/jest/pull/6244))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better late than never?

@UselessPickles
Copy link
Contributor Author

@SimenB @rickhanlonii Ready for review again.

@UselessPickles
Copy link
Contributor Author

bump

It's been oddly quiet here for a while. Has this PR been forgotten, or is it just low priority?

@UselessPickles
Copy link
Contributor Author

@SimenB @rickhanlonii poke

@UselessPickles
Copy link
Contributor Author

@rickhanlonii @SimenB Is there any chance this can be reviewed/merged soon?

@UselessPickles
Copy link
Contributor Author

@SimenB @rickhanlonii What's the next step on this PR? Do you want me to change the entry in the changelog? I can't really comment on what the entry should be.

@thymikee
Copy link
Collaborator

thymikee commented Aug 7, 2018

@SimenB mind taking a final look at this one?

@SimenB
Copy link
Member

SimenB commented Sep 29, 2018

@rickhanlonii next release is a major, mind making the breaking changes we want and land this?

@SimenB SimenB added this to the Jest 24 milestone Oct 16, 2018
@thymikee
Copy link
Collaborator

ping @SimenB @rickhanlonii

@SimenB
Copy link
Member

SimenB commented Oct 30, 2018

Might work to just revert/remove the last few commits so we're back to the original code submitted in this PR?

@UselessPickles
Copy link
Contributor Author

Do we just want to remove the "deprecated" isThrow property? I can take care of that tonight. I haven't thought about this in a long time, so I can't remember if there were any other breaking changes that would be nice to do now while we have the chance.

@SimenB
Copy link
Member

SimenB commented Oct 30, 2018

Yeah, that should be it!

@SimenB
Copy link
Member

SimenB commented Oct 31, 2018

Perfect, thank you so much! And sorry about the churn

@SimenB SimenB merged commit bb9efac into jestjs:master Oct 31, 2018
@UselessPickles UselessPickles deleted the recursive_mock_results branch October 31, 2018 23:43
@rubennorte
Copy link
Contributor

rubennorte commented Nov 9, 2018

Hey @UselessPickles, I just saw this PR after releasing 24.0.0-alpha.6. Would you mind updating the PR (mainly the title) to better reflect the implications of the changes you did here? In this case, the structure of mock objects have changed for end-users and that's not clear by reading this. E.g.:

Before:

expect(callbackOne.mock.results[0].isThrow).toBe(true);

After:

expect(callbackOne.mock.results[0].type).toBe('throw');

I can edit the PR but I prefer if the original author can do it. I'll update the changelog soon so you don't have to worry about that.

@UselessPickles
Copy link
Contributor Author

I think the PR title accurately describes the purpose of this PR. The PR description clearly explains the breaking change. The entry in the change log clearly indicates that there is a breaking change. I see nothing wrong.

If the goal is to compile a list of breaking changes and a migration guide, then all the necessary information is already there.

I don't know what you would expect from the title to both describe the PR purpose and indicate breaking changes. Feel free to change it.

@rubennorte
Copy link
Contributor

Perhaps I didn't use the better wording here. I think this should've been 2 different PRs: one to change the mock interface and another to fix their behaviour with recursive functions. The most relevant change here is the change in the interface, even though your initial goal with this PR was to fix the issue.

@UselessPickles
Copy link
Contributor Author

The breaking change came about during the PR review process (at the request of a reviewer). It was not originally a breaking change, and even the design of the breaking change changed over time (at one point retaining deprecated backward compatibility, etc.). I don't think it would have been practical to separate it into two PRs until the very end when the entire change was approved. Hindsight, etc.

Again... I really don't know what you expect from an updated title, so I'm the wrong person to make the change. You can update the title/description however you see fit to clarify the breaking change. I won't be offended :)

@github-actions
Copy link

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 May 12, 2021
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.

None yet

7 participants