jest: [Bug]: globalsCleanup causes issues with some deps, can't be properly turned off
Version
30.0.0
Steps to reproduce
git clone https://github.com/EDM115-alt/repro-5.git
pnpm i
pnpm test
If you can't run the commands for some reason, here's the output :
PS C:\Users\EDM115\Desktop\repro-5> pnpm test
> repro@1.0.0 test C:\Users\EDM115\Desktop\repro-5
> run-s --continue-on-error test:base test:noCleanup test:hackishFix
> repro@1.0.0 test:base C:\Users\EDM115\Desktop\repro-5
> node --experimental-vm-modules --expose-gc ./node_modules/jest/bin/jest.js --runInBand --logHeapUsage --config ./jest.config.base.js
Determining test suites to run...Server built
FAIL ./test.js
● Test suite failed to run
TypeError: Cannot assign to read only property 'Symbol($$jest-protect-from-deletion)' of object '[object Object]'
at Function.assign (<anonymous>)
at new T (node_modules/.pnpm/@influxdata+influxdb3-client@1.1.0/node_modules/node_modules/@protobuf-ts/runtime/build/es2015/message-type.js:26:74)
at new Ve (node_modules/.pnpm/@influxdata+influxdb3-client@1.1.0/node_modules/@influxdata/influxdb3-client/src/generated/flight/google/protobuf/timestamp.ts:157:9)
at node_modules/.pnpm/@influxdata+influxdb3-client@1.1.0/node_modules/@influxdata/influxdb3-client/src/generated/flight/google/protobuf/timestamp.ts:237:26
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.442 s
Ran all test suites.
Server closed
(node:2404) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ELIFECYCLE Command failed with exit code 1.
> repro@1.0.0 test:noCleanup C:\Users\EDM115\Desktop\repro-5
> node --experimental-vm-modules --expose-gc ./node_modules/jest/bin/jest.js --runInBand --logHeapUsage --config ./jest.config.noCleanup.js
Determining test suites to run...Server built
FAIL ./test.js
● Test suite failed to run
TypeError: Cannot assign to read only property 'Symbol($$jest-protect-from-deletion)' of object '[object Object]'
at Function.assign (<anonymous>)
at new T (node_modules/.pnpm/@influxdata+influxdb3-client@1.1.0/node_modules/node_modules/@protobuf-ts/runtime/build/es2015/message-type.js:26:74)
at new Ve (node_modules/.pnpm/@influxdata+influxdb3-client@1.1.0/node_modules/@influxdata/influxdb3-client/src/generated/flight/google/protobuf/timestamp.ts:157:9)
at node_modules/.pnpm/@influxdata+influxdb3-client@1.1.0/node_modules/@influxdata/influxdb3-client/src/generated/flight/google/protobuf/timestamp.ts:237:26
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.44 s
Ran all test suites.
Server closed
(node:19220) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ELIFECYCLE Command failed with exit code 1.
> repro@1.0.0 test:hackishFix C:\Users\EDM115\Desktop\repro-5
> node --experimental-vm-modules --expose-gc ./node_modules/jest/bin/jest.js --runInBand --logHeapUsage --config ./jest.config.hackishFix.js
Determining test suites to run...Server built
console.log
Server built
at log (server.js:9:11)
(node:21236) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS ./test.js (38 MB heap size)
√ working (2 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.47 s, estimated 1 s
Ran all test suites.
Server closed
ERROR: "test:noCleanup" exited with 1.
ELIFECYCLE Test failed. See above for more details.
Expected behavior
The property deletion feature introduced in Jest 30 (https://github.com/jestjs/jest/blob/main/packages/jest-util/src/garbage-collection-utils.ts) is a great idea, and marking stuff as protected to not delete them is great too !
However it seems like it tries to edit immutable objects, causing the tests to fail.
And disabling the testEnvironmentOptions.globalsCleanup should prevent this behavior altogether (like in Jest <30) but it doesn’t.
Actual behavior
Some external functions (like here from @influxdata/influxdb3-client) gets marked as protected from deletion which is nice, but uh-oh it can’t be edited :
Cannot assign to read only property 'Symbol($$jest-protect-from-deletion)' of object '[object Object]'
Setting testEnvironmentOptions.globalsCleanup to “off” doesn’t change things, and we have to use a hackish fix :
- Create a file containing this :
// jest.protect-fix.js const proto = Object.prototype const sym = Object.getOwnPropertySymbols(proto).find( (s) => String(s) === "Symbol($$jest-protect-from-deletion)", ) if (sym) { delete proto[sym] } - Use it among the
setupFilesof your config since they run before theglobalSetupas it seems :const config = { testEnvironment: "node", setupFiles: ["<rootDir>/jest.protect-fix.js"], testTimeout: 5000, globalSetup: "<rootDir>/setup.js", globalTeardown: "<rootDir>/teardown.js", modulePathIgnorePatterns: ["<rootDir>/node_modules"], } export default config
Additional context
We had this issue using ts-jest and TS files but in the repro it is also happening with bare JS
Environment
System:
OS: Windows 11 10.0.26100
CPU: (12) x64 12th Gen Intel(R) Core(TM) i7-1255U
Binaries:
Node: 22.16.0 - C:\nvm4w\nodejs\node.EXE
npm: 11.4.1 - C:\nvm4w\nodejs\npm.CMD
pnpm: 10.11.0 - ~\AppData\Local\pnpm\pnpm.CMD
npmPackages:
jest: ^30.0.0 => 30.0.0
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 16 (8 by maintainers)
Commits related to this issue
- #15678 Run jest-environment-node tests with all combinations of 'globalsCleanup' configuration — committed to eyalroth/jest by eyalroth a year ago
- #15678 wip — committed to eyalroth/jest by eyalroth a year ago
- #15678 Globals cleanup: avoid setting protection symbol when feature is off — committed to eyalroth/jest by eyalroth a year ago
- #15678 Changelog — committed to eyalroth/jest by eyalroth a year ago
- #15678 Run jest-environment-node tests with all combinations of 'globalsCleanup' configuration — committed to eyalroth/jest by eyalroth a year ago
- #15678 Globals cleanup: avoid setting protection symbol when feature is off — committed to eyalroth/jest by eyalroth a year ago
- #15678 Changelog — committed to eyalroth/jest by eyalroth a year ago
- #15678 [jest-matcher-utils] Make 'deepCyclicCopyObject' safer by setting descriptors to null-prototype object — committed to eyalroth/jest by eyalroth a year ago
- #15678 [jest-util] Make garbage collection protection property writable — committed to eyalroth/jest by eyalroth a year ago
- #15678 Changelog — committed to eyalroth/jest by eyalroth a year ago
- #15678 Changelog — committed to eyalroth/jest by eyalroth a year ago
Published Jest 30.0.1 which should hopefully resolve the issue if you turn the cleanup mode to off.
Just to quickly explain, we, and specifically @eyalroth, have been trying to identify ways to reduce memory leaks that are caused by user code in tests that cannot be cleaned up because things attach themselves to global state and stick around for the full test run, even across individual tests since we re-use processes/threads. Unfortunately in JavaScript it is hard to do that safely. Thank you for your bug reports that help inform the direction to go in.
@cpojer It’s not.
The package is trying to create a new prototype that is identical to Object’s prototype, but with an additional property.
They read all of Object’s prototype property descriptors (with
Object.getOwnPropertyDescriptors) and then try to assign them to a new empty object ({}). It works okay with the spread operator ({...ojb}) but not withObject.assign).Thinking about it now, that might actually make all of their messages protected from cleanup 😬
Closing the loop here - The issue was resolved after
elastic-builderwas updated to use the latest (and patched) version of Lodash.@khitrenovich I’m afraid there’s not enough information here for me to identify the issue.
Could you please provide more information and preferability a minimal reproducible project?
We are running into similar issues comparing POJOs coming from
elastic-builder’stoJSON()override (see sample below). Is that an issue with the matcher implementation or something I should report to theelastic-buildermaintainers?For me, this also throws the error:
Environment:
Let’s just say my “huh!?” was immeasurable, hah.
This looks like a bug in protobuf-ts.
I will open a bug report for them.
In the meanwhile I’ll open a PR to avoid setting the protection symbol when
globalsCleanup: off.Bug details
While their source code uses the spread operator:
their build actually transpiles it to:
Eventually, this code is problematic:
What it does is:
Object’s prototype, which include the new Jest deletion symbol.{ <propertyKey>: <propertyDefinition> }{})I don’t think there is any guarantee that Object’s prototype props will all be writable, so their code is dangerous regardless of Jest.