Skip to content

fix(jest-runtime): Guard '_isMockFunction' access with 'in' - #14188

Merged
SimenB merged 4 commits into
jestjs:mainfrom
rbuckton:patch-1
Jun 21, 2023
Merged

SimenB merged 4 commits into
jestjs:mainfrom
rbuckton:patch-1

Conversation

@rbuckton

@rbuckton rbuckton commented Jun 8, 2023 •

Copy link
Copy Markdown

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

linux-foundation-easycla Bot commented Jun 8, 2023 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@netlify

netlify Bot commented Jun 8, 2023 •

Copy link
Copy Markdown

βœ… 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

@SimenB SimenB left a comment

Copy link
Copy Markdown
Member

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

SimenB commented Jul 4, 2023

Copy link
Copy Markdown
Member

@github-actions

github-actions Bot commented Aug 4, 2023

Copy link
Copy Markdown

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