Skip to content

jsdom: localStorage/sessionStorage dropped by getWindowKeys on Node 25+ (follow-up to #8757) #10867

Closed as not planned

Description

@laudatdsgn

Follow-up to #8757, which was closed with "This looks like a bug of Node 25 […]
non-LTS is not officially supported by Vitest". Two things have changed since,
and I think they justify a second look:

  1. The Node-side issue it pointed at, localStorage's behaviour when no or invalid --localstorage-file provided nodejs/node#60303, is now closed as
    completed
    (2026-01-22) — and the Vitest symptom persists unchanged on
    Node 26.3.0. So "wait for Node" has played out and did not resolve it.
  2. Node 24 is LTS and 26 is current, so the non-LTS argument has expired.
    Every project that moves to a supported modern Node hits this.

I also think the original attribution was incomplete. Node's behaviour here is
legitimate; the reason jsdom's Storage never reaches the test is a specific
choice in Vitest's own key-copying.

Mechanism

getWindowKeys in packages/vitest/src/integrations/env/utils.ts:

return new Set(keysArray.concat(Object.getOwnPropertyNames(win)).filter((k) => {
  if (skipKeys.includes(k)) return false;
  if (k in global) return keysArray.includes(k);   // <-- here
  return true;
}));

A key already present on globalThis is only copied if it is in the allowlist.
Node 25+ ships localStorage/sessionStorage as configurable accessors, so
'localStorage' in globalThis is true — and neither key is in LIVING_KEYS
or OTHER_KEYS. The jsdom window's Storage is therefore filtered out, and
window.localStorage ends up undefined.

Verified against the installed vitest@4.1.10 bundle:

LIVING_KEYS | localStorage: false | sessionStorage: false
OTHER_KEYS  | localStorage: false | sessionStorage: false
skipKeys    | localStorage: false | sessionStorage: false
$ node -v
v26.3.0
$ node -e "console.log('in globalThis:', 'localStorage' in globalThis, '| typeof:', typeof globalThis.localStorage)"
in globalThis: true | typeof: undefined

jsdom itself is fine — a directly constructed window round-trips:

$ node --input-type=module -e "
import { JSDOM } from 'jsdom'
const d = new JSDOM('', { url: 'http://localhost:3000/' })
d.window.localStorage.setItem('a','b')
console.log(typeof d.window.localStorage, d.window.localStorage.getItem('a'))
"
object b

Reproduction

// probe.test.ts
// @vitest-environment jsdom
import { expect, it } from 'vitest'

it('has localStorage', () => {
  expect(typeof window.localStorage).toBe('object') // "undefined"
})

Suggested fix

Add both keys to OTHER_KEYS, so the k in global branch returns true:

   'length',
+  'localStorage',
+  'sessionStorage',

Why this is worth fixing rather than documenting

There is no error and no warning from Vitest — only undefined. The failure
surfaces wherever a test first dereferences storage, which is usually a
beforeEach, so it reads as a broken test. In our case two suites were dead for
weeks and the resulting failures were written off as pre-existing before anyone
traced them here.

Workaround, for anyone landing here

--no-webstorage drops Node's global so jsdom's copy lands normally. It has to
be version-guarded: Node 22 rejects the flag outright with bad option, and
does not need it (Web Storage is not enabled there).

// vite.config.ts / vitest.config.ts
test: {
  execArgv:
    Number(process.versions.node.split('.')[0]) >= 25
      ? ['--no-webstorage']
      : [],
}

Version

vitest 4.1.10 · jsdom 30.0.1 · Node 26.3.0

Activity

  1. FrancoKaddour commented on Aug 4, 2026

    @FrancoKaddour

    This is already fixed on main for v5 by #10293 — localStorage/sessionStorage were added to the jsdom key allowlist, and populateGlobal now snapshots property descriptors so Node's lazy getter is never invoked. Since v5 is still in beta and 4.1.x is the current stable line, I've prepared a backport of the environment portion of that change per the release-branch policy: #10873

  2. sheremet-va commented on Aug 5, 2026

    @sheremet-va
    Member

    as mentioned by @FrancoKaddour, this is fixed in Vitest 5. It can't be backported because it requires a breaking change

  3. locked and limited conversation to collaborators on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions