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 issue mocking bound method #3805

Merged
merged 1 commit into from
Jun 13, 2017
Merged

Fix issue mocking bound method #3805

merged 1 commit into from
Jun 13, 2017

Conversation

xadn
Copy link
Contributor

@xadn xadn commented Jun 13, 2017

Binding and mocking methods in a specific order causes a Maximum call stack size exceeded error when the mocked method is called.

Summary
_createMockFunction normally detects functions that have already been mocked by checking if they are named "mockConstructor". However, calling bind on the mocked function changes the function name and causes the check to fail.

_createMockFunction already has code to strip out the "bound " prefix, but the check to look for "mockConstructor" was happening too early. This PR moves delays checking for "mockConstructor" until after "bound " has already been stripped out.

Repro steps:

  1. Mock an anonymous function (fn is now named "mockConstructor")
  2. Bind the mocked function (fn is now named name "bound mockConstructor")
  3. Mock the bound function (fn fails check "bound mockConstructor" === "mockConstructor" and gets into an infinite loop)

Test plan

  • yarn test
  • cd ~/www; jest --all ;)

@codecov-io
Copy link

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3805      +/-   ##
==========================================
+ Coverage   57.58%   57.59%   +0.01%     
==========================================
  Files         194      194              
  Lines        6776     6778       +2     
  Branches        6        6              
==========================================
+ Hits         3902     3904       +2     
  Misses       2871     2871              
  Partials        3        3
Impacted Files Coverage Δ
packages/jest-mock/src/index.js 91.92% <100%> (+0.07%) ⬆️

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 ec78c96...4b3c2d8. Read the comment docs.

@cpojer cpojer merged commit c8c74f4 into jestjs:master Jun 13, 2017
@cpojer
Copy link
Member

cpojer commented Jun 13, 2017

Thanks!

tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
@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 13, 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.

4 participants