@@ -11,30 +11,40 @@ const denyList = new Set([
11
11
'Uint8Array' ,
12
12
] )
13
13
14
- const nodeGlobals = new Map (
15
- Object . getOwnPropertyNames ( globalThis )
16
- . filter ( global => ! denyList . has ( global ) )
17
- . map ( ( nodeGlobalsKey ) => {
14
+ const nodeGlobals = new Map ( )
15
+
16
+ function populateNodeGlobals ( ) {
17
+ if ( nodeGlobals . size !== 0 ) {
18
+ return
19
+ }
20
+
21
+ const names = Object . getOwnPropertyNames ( globalThis )
22
+ const length = names . length
23
+ for ( let i = 0 ; i < length ; i ++ ) {
24
+ const globalName = names [ i ]
25
+ if ( ! denyList . has ( globalName ) ) {
18
26
const descriptor = Object . getOwnPropertyDescriptor (
19
27
globalThis ,
20
- nodeGlobalsKey ,
28
+ globalName ,
21
29
)
22
30
23
31
if ( ! descriptor ) {
24
32
throw new Error (
25
- `No property descriptor for ${ nodeGlobalsKey } , this is a bug in Vitest.` ,
33
+ `No property descriptor for ${ globalName } , this is a bug in Vitest.` ,
26
34
)
27
35
}
28
-
29
- return [ nodeGlobalsKey , descriptor ]
30
- } ) ,
31
- )
36
+ nodeGlobals . set ( globalName , descriptor )
37
+ }
38
+ }
39
+ }
32
40
33
41
export default < Environment > {
34
42
name : 'node' ,
35
43
viteEnvironment : 'ssr' ,
36
44
// this is largely copied from jest's node environment
37
45
async setupVM ( ) {
46
+ populateNodeGlobals ( )
47
+
38
48
const vm = await import ( 'node:vm' )
39
49
let context = vm . createContext ( )
40
50
let global = vm . runInContext ( 'this' , context )
0 commit comments