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(jest-runtime): Guard '_isMockFunction' access with 'in' #14188

Merged
merged 4 commits into from
Jun 21, 2023

Conversation

rbuckton
Copy link
Contributor

@rbuckton rbuckton commented Jun 8, 2023

Summary

This fixes an issue in jest-runtime when attempting to reset global mocks. When jest is running inside of VS Code to test a VS Code extension, it attempts to read _isMockFunction from a Proxy that happens to throw in its get handler when you unconditionally read a property that does not exist. Prefixing this condition with a test for in avoids evaluating the get hook.

NOTE: While this does resolve this specific case, it's still possible a global could be a Proxy that even throws on in, though that would generally be a bad practice. It would be much safer to replace _isMockFunction entirely with a WeakSet that does not interact with a Proxy at all, but that is a far more comprehensive change than I'm willing to commit to at this point.

Test plan

I've tested this locally through the use of a custom JestEnvironment, such as this one:

import NodeEnvironment from "jest-environment-node";
export default class extends NodeEnvironment {
  async setup() {
    await super.setup();
    this.global.UNSAFE_GLOBAL = new Proxy({}, {
      get(target, p, receiver) {
        if (p === "_isMockFunction") throw new Error();
      }
    });
  }
  async teardown() {
    this.global.UNSAFE_GLOBAL = null;
    await super.teardown();
  }
}

I've added something similar to the above as a test in this PR.

Fixes #14095

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jun 8, 2023

CLA Signed

The committers listed above are authorized under a signed CLA.

@netlify
Copy link

netlify bot commented Jun 8, 2023

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 92cfa13
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/6492bb4ee88de20008bbe6e1
😎 Deploy Preview https://deploy-preview-14188--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@rbuckton rbuckton force-pushed the patch-1 branch 2 times, most recently from fc3d89e to 2c9e9c5 Compare June 8, 2023 20:38
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.

clean fix, thanks!

I'd be interested in the WeakSet change you mention if you ever find the time! 😀

@SimenB SimenB merged commit ca0557b into jestjs:main Jun 21, 2023
@SimenB
Copy link
Member

SimenB commented Jul 4, 2023

@github-actions
Copy link

github-actions bot commented Aug 4, 2023

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 Aug 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Jest 28.0.0 breaks vscode Jest testing with "Cannot find module '_isMockFunction'
2 participants