Skip to content

Commit a5ec316

Browse files
authored
fix(vm): keep assignable accessor globals writable in node environment (#11834)
Signed-off-by: Martin Oehlert <[email protected]> Co-authored-by: Martin Oehlert <[email protected]>
1 parent e46c6c1 commit a5ec316

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

‎packages/vitest/src/integrations/env/node.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ export default <Environment>{
5454
configurable: true,
5555
enumerable: descriptor.enumerable,
5656
value: val,
57-
writable:
58-
descriptor.writable === true ||
59-
// Node 19 makes performance non-readable. This is probably not the correct solution.
60-
nodeGlobalsKey === 'performance',
57+
// accessor globals like `performance` and `localStorage` (Node 25+) are assignable when they have a setter
58+
writable: descriptor.writable === true || typeof descriptor.set === 'function',
6159
})
6260
return val
6361
},

‎test/e2e/test/vm-threads.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,3 +966,31 @@ test('prewarm follows __mocks__ redirects and setup file mocks', async () => {
966966
]
967967
`)
968968
})
969+
970+
// Node 25+ exposes `localStorage` and `sessionStorage` as accessors with a
971+
// setter, so reading the lazy global must not make it read-only
972+
test.for(['vmThreads', 'vmForks'] as const)(
973+
'%s keeps assignable accessor globals writable after reading them',
974+
async (pool) => {
975+
const { stderr, exitCode } = await runInlineTests(
976+
{
977+
'basic.test.js': `
978+
import { expect, test } from 'vitest'
979+
980+
test('read then assign', () => {
981+
for (const key of ['performance', 'localStorage', 'sessionStorage']) {
982+
void globalThis[key]
983+
const value = {}
984+
globalThis[key] = value
985+
expect(globalThis[key]).toBe(value)
986+
}
987+
})
988+
`,
989+
},
990+
{ pool, environment: 'node' },
991+
)
992+
993+
expect(stderr).not.toContain('Cannot')
994+
expect(exitCode).toBe(0)
995+
},
996+
)

0 commit comments

Comments
 (0)