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

jest.resetModules() can fail when using identity-obj-proxy #7009

Closed
danrr opened this issue Sep 20, 2018 · 4 comments · Fixed by #7017
Closed

jest.resetModules() can fail when using identity-obj-proxy #7009

danrr opened this issue Sep 20, 2018 · 4 comments · Fixed by #7017

Comments

@danrr
Copy link

danrr commented Sep 20, 2018

🐛 Bug Report

jest.resetModules() iterates over the global object and checks if any properties are mocks by evaluating if globalMock._isMockFunction is truthy for each property on the global. (https://github.com/facebook/jest/blob/949701376f1af75befd1a759aff5ac8080fb33af/packages/jest-runtime/src/index.js#L433)

If using identity-obj-proxyto mock a property on the global object anywhere in the test setup, jest.resetModules will break. identity-obj-proxy is a proxy that returns the key as the value for any attemps to index the object (https://github.com/keyanzhang/identity-obj-proxy/blob/master/src/index.js#L18) which means the value of _isMockFunction will be the string _isMockFunction which is truthy, and the value of mockClear will be the string mockClear which is not a function, ergo the error TypeError: globalMock.mockClear is not a function.

To Reproduce

Steps to reproduce the behavior:

  • Set a global property to be identity-obj-proxy
  • Call jest.resetModules() during a test

Expected behavior

Test should pass, correct mocks should be cleared

Link to repl or repo (highly encouraged)

https://repl.it/@danrr/SuperNegligibleGeeklog

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz
  Binaries:
    Node: 8.11.4 - ~/.nvm/versions/node/v8.11.4/bin/node
    npm: 5.6.0 - ~/.nvm/versions/node/v8.11.4/bin/npm
  npmPackages:
    jest: ^23.4.1 => 23.5.0 
@SimenB
Copy link
Member

SimenB commented Sep 20, 2018

Interesting! We might want to consider som Symbol to make the check safer.

I've thought that I want to move all properties we attach to a mock behind a symbol anyways, but it's pretty breaking.

@thymikee @rickhanlonii thoughts?

@danrr
Copy link
Author

danrr commented Sep 20, 2018

Can the globalMock._isMockFunction check be changed to globalMock._isMockFunction === true to fix this issue in the short term?

@SimenB
Copy link
Member

SimenB commented Sep 20, 2018

for sure. PR welcome 🙂

danrr pushed a commit to danrr/jest that referenced this issue Sep 21, 2018
danrr pushed a commit to danrr/jest that referenced this issue Sep 21, 2018
danrr pushed a commit to danrr/jest that referenced this issue Sep 23, 2018
danrr pushed a commit to danrr/jest that referenced this issue Sep 28, 2018
danrr pushed a commit to danrr/jest that referenced this issue Sep 28, 2018
danrr pushed a commit to danrr/jest that referenced this issue Sep 28, 2018
@github-actions
Copy link

This issue 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 a pull request may close this issue.

2 participants