jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: Jest fails with localstorage error with node 25.2.0

Open shadowspawn opened this issue 1 month ago • 9 comments

Version

30.2.0

Steps to reproduce

Reproduction steps:

  1. Clone https://github.com/shadowspawn/jest-local-storage
  2. using node 25.2.0...
  3. npm install
  4. npx jest

Expected behavior

I expect the tests to run and pass.

Actual behavior

% npx jest
 FAIL  ./index.test.js
  ā— Test suite failed to run

    SecurityError: Cannot initialize local storage without a `--localstorage-file` path

      at new NodeEnvironment (node_modules/jest-environment-node/build/index.js:110:52)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.029 s
Ran all test suites.

Additional context

I am using testEnvironment: "node".

There was a change of behaviour in https://github.com/nodejs/node/pull/60351

This failed the node CITGM test for Commander: https://github.com/nodejs/node/pull/60677#issuecomment-3518940754

After reproducing, I opened an issue for Commander: https://github.com/tj/commander.js/issues/2451

And then added a reproduction here!

Environment

System:
    OS: macOS 26.1
    CPU: (10) arm64 Apple M1 Pro
  Binaries:
    Node: 25.2.0 - /usr/local/bin/node
    npm: 11.6.2 - /usr/local/bin/npm
    bun: 1.2.1 - /opt/homebrew/bin/bun
    Deno: 2.1.7 - /opt/homebrew/bin/deno
  npmPackages:
    jest: ^30.2.0 => 30.2.0

shadowspawn avatar Nov 12 '25 06:11 shadowspawn

i fixed it by:

  1. npm install --save-dev jest-environment-jsdom
  2. adding to jest.config.ts: testEnvironment: "jsdom",

orianhexight avatar Nov 12 '25 09:11 orianhexight

(In my case, I am testing in node environment. I will add that to the context in description, thanks.)

shadowspawn avatar Nov 12 '25 09:11 shadowspawn

From the original PR's thread, I can see that the introduced feature/issue is still experimental and not really meant for stable userland. Having said that, one folk did say this one is worthy of semver-major so there's that.

There could be further modifications to this one in Node.js runtime so I'm not sure what's to put out as a guardrail, might be a try-catch when accessing globalThis attribute?

hainenber avatar Nov 12 '25 15:11 hainenber

This solved it for me: I set the localStorage file path via NODE_OPTIONS like this: NODE_OPTIONS='--localstorage-file=./jest-storage' jest

Update: For me this issue started because we were using "latest" as version for node in the pipelines. This caused node version 25.2.0 to be used, which ultimately caused the issue. That error throwing was added here #60351

apravehum avatar Nov 13 '25 10:11 apravehum

Reported to Node.js - https://github.com/nodejs/node/issues/60704, this is a breaking change

alexander-akait avatar Nov 13 '25 14:11 alexander-akait

Wonderful šŸ˜…

SimenB avatar Nov 14 '25 14:11 SimenB

It is fixed in node v25.2.1

fabasoad avatar Nov 18 '25 07:11 fabasoad

We'll still need to fix it tho, as it'll be in v26

SimenB avatar Nov 18 '25 08:11 SimenB

Or it might be fixed upstream, let's see how https://github.com/nodejs/node/issues/60303 shakes out

SimenB avatar Nov 18 '25 08:11 SimenB

I’d be happy to take this one on and prepare a fix @SimenB could you please assign me to this issue?

Thanks!

bas0N avatar Dec 12 '25 00:12 bas0N