@@ -11,30 +11,40 @@ const denyList = new Set([
1111 'Uint8Array' ,
1212] )
1313
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 ) ) {
1826 const descriptor = Object . getOwnPropertyDescriptor (
1927 globalThis ,
20- nodeGlobalsKey ,
28+ globalName ,
2129 )
2230
2331 if ( ! descriptor ) {
2432 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.` ,
2634 )
2735 }
28-
29- return [ nodeGlobalsKey , descriptor ]
30- } ) ,
31- )
36+ nodeGlobals . set ( globalName , descriptor )
37+ }
38+ }
39+ }
3240
3341export default < Environment > {
3442 name : 'node' ,
3543 viteEnvironment : 'ssr' ,
3644 // this is largely copied from jest's node environment
3745 async setupVM ( ) {
46+ populateNodeGlobals ( )
47+
3848 const vm = await import ( 'node:vm' )
3949 let context = vm . createContext ( )
4050 let global = vm . runInContext ( 'this' , context )
0 commit comments