Skip to content

Fix destruction of builtin node globals #15636

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

Merged
merged 1 commit into from
May 29, 2025

Conversation

eyalroth
Copy link
Contributor

@eyalroth eyalroth commented May 28, 2025

Summary

Fixes #15632 that was introduced in #15215.

The PR to mitigate memory leaks accidentally deleted some of the builtin node globals.

This PR fixes it by protecting all of them from deletion.

Test plan

New tests were added that rely on the reproduction project from #15632.

Looks like one CI test (e2e/__tests__/circusConcurrent.test.ts) fails, but from the output it seems to be flaky?

I see previous builds where it fails several times but succeeds on the last (3rd) attempt.

Copy link

netlify bot commented May 28, 2025

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 7b33efa
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/68377bf9648cbd0009e64ae1
😎 Deploy Preview https://deploy-preview-15636--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 project configuration.

@eyalroth eyalroth changed the title #15632 Fix destruction of builtin node globals Fix destruction of builtin node globals May 28, 2025
@eyalroth eyalroth marked this pull request as ready for review May 28, 2025 16:00
@@ -102,6 +102,7 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
Object.getOwnPropertyNames(global) as GlobalProperties,
);
for (const [nodeGlobalsKey, descriptor] of nodeGlobals) {
protectProperties(global[nodeGlobalsKey]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to fix #15635 if we change this line to:

Suggested change
protectProperties(global[nodeGlobalsKey]);
protectProperties(globalThis[nodeGlobalsKey]);

Copy link
Contributor Author

@eyalroth eyalroth May 28, 2025

Choose a reason for hiding this comment

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

@lencioni Yes that was actually my original code which I tested on. global is the variable from line 95 which at this point doesn't even have nodeGlobalsKey, and so protectProperties won't do a thing.

I think what happens is that in my files I was editing the compiled file (index.js) to avoid waiting for complication every time, and when I moved the code to the source file (index.ts) I accidentally changed the variable (AI auto complete? that would be a conspiracy).

In any case, it worries me that the CI tests don't pick on this, because the unit tests that I added definitely fail on this:

$ yarn test -- jest-environment-node

 PASS  packages/jest-environment-node/src/__tests__/node_environment.test.ts
 FAIL  packages/jest-environment-node/src/__tests__/node_environment_2.test.ts
  ● NodeEnvironment 2 › dispatch event

    TypeError: Value of "this" must be of type EventTarget

      at Object.dispatchEvent (packages/jest-environment-node/src/__tests__/node_environment_2.test.ts:10:23)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 7 passed, 8 total
Snapshots:   0 total
Time:        0.331 s, estimated 1 s
Ran all test suites matching jest-environment-node.

(and also yarn test locally)

In any case, I will amend the commit.

@eyalroth eyalroth force-pushed the 15632-fix-destruction-node-globals branch from c3cee80 to 7b33efa Compare May 28, 2025 21:11
@eyalroth
Copy link
Contributor Author

PR updated with amend + force push (see comment conversation).

@cpojer FYI it looks like there are issues with the CI tests:

  1. e2e/__tests__/circusConcurrent.test.ts is flaky (see PR description)
  2. Unit tests that fail locally (yarn test or yarn test -- jest-environment-node) pass in CI

yarn test -- jest-environment-node targets 2 test files that are supposed to fail only when both are run together. Could it be that CI shards/splits them and therefore they pass?

Copy link
Member

@cpojer cpojer left a comment

Choose a reason for hiding this comment

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

Wow, thanks everyone for being so on top of things!

@cpojer cpojer merged commit 973c574 into jestjs:main May 29, 2025
85 checks passed
eyalroth added a commit to eyalroth/jest that referenced this pull request Jun 5, 2025
…anup on env teardown (to reduce memory leaks)
cpojer pushed a commit that referenced this pull request Jun 5, 2025
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 Jun 29, 2025
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 30 beta causes random ERR_INVALID_THIS errors
3 participants