@@ -288,6 +288,7 @@ class WPTRunner {
288288 this . resource = new ResourceLoader ( path ) ;
289289
290290 this . flags = [ ] ;
291+ this . dummyGlobalThisScript = null ;
291292 this . initScript = null ;
292293
293294 this . status = new StatusLoader ( path ) ;
@@ -318,6 +319,43 @@ class WPTRunner {
318319 this . initScript = script ;
319320 }
320321
322+ get fullInitScript ( ) {
323+ if ( this . initScript === null && this . dummyGlobalThisScript === null ) {
324+ return null ;
325+ }
326+
327+ if ( this . initScript === null ) {
328+ return this . dummyGlobalThisScript ;
329+ } else if ( this . dummyGlobalThisScript === null ) {
330+ return this . initScript ;
331+ }
332+
333+ return `${ this . fullInitScript } \n\n//===\n${ this . initScript } ` ;
334+ }
335+
336+ /**
337+ * Pretend the runner is run in `name`'s environment (globalThis).
338+ * @param {'Window' } name
339+ * @see {@link https://github.com/nodejs/node/blob/24673ace8ae196bd1c6d4676507d6e8c94cf0b90/test/fixtures/wpt/resources/idlharness.js#L654-L671 }
340+ */
341+ pretendGlobalThisAs ( name ) {
342+ switch ( name ) {
343+ case 'Window' : {
344+ this . dummyGlobalThisScript =
345+ 'global.Window = Object.getPrototypeOf(globalThis).constructor;' ;
346+ break ;
347+ }
348+
349+ // TODO(XadillaX): implement `ServiceWorkerGlobalScope`,
350+ // `DedicateWorkerGlobalScope`, etc.
351+ //
352+ // e.g. `ServiceWorkerGlobalScope` should implement dummy
353+ // `addEventListener` and so on.
354+
355+ default : throw new Error ( `Invalid globalThis type ${ name } .` ) ;
356+ }
357+ }
358+
321359 // TODO(joyeecheung): work with the upstream to port more tests in .html
322360 // to .js.
323361 runJsTests ( ) {
@@ -368,7 +406,7 @@ class WPTRunner {
368406 testRelativePath : relativePath ,
369407 wptRunner : __filename ,
370408 wptPath : this . path ,
371- initScript : this . initScript ,
409+ initScript : this . fullInitScript ,
372410 harness : {
373411 code : fs . readFileSync ( harnessPath , 'utf8' ) ,
374412 filename : harnessPath ,
0 commit comments