Repository navigation
Description
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:
- The Node-side issue it pointed at,
localStorage's behaviour when no or invalid--localstorage-fileprovided 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. - 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
FrancoKaddour commentedon Aug 4, 2026
This is already fixed on
mainfor v5 by #10293 —localStorage/sessionStoragewere added to the jsdom key allowlist, andpopulateGlobalnow 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: #10873sheremet-va commentedon Aug 5, 2026
as mentioned by @FrancoKaddour, this is fixed in Vitest 5. It can't be backported because it requires a breaking change
test: pass Section 0 setup gates; fix 4 baseline blockers