@@ -478,6 +478,28 @@ function buildProjectWorkerOptions(
478478 runnerWorker . compatibilityFlags . push ( "unsafe_module" ) ;
479479 }
480480
481+ // The following nodejs compat flags enable features required for Vitest to work properly
482+ ensureFlag (
483+ runnerWorker . compatibilityFlags ,
484+ "enable_nodejs_tty_module" ,
485+ "disable_nodejs_tty_module"
486+ ) ;
487+ ensureFlag (
488+ runnerWorker . compatibilityFlags ,
489+ "enable_nodejs_fs_module" ,
490+ "disable_nodejs_fs_module"
491+ ) ;
492+ ensureFlag (
493+ runnerWorker . compatibilityFlags ,
494+ "enable_nodejs_http_modules" ,
495+ "disable_nodejs_http_modules"
496+ ) ;
497+ ensureFlag (
498+ runnerWorker . compatibilityFlags ,
499+ "enable_nodejs_perf_hooks_module" ,
500+ "disable_nodejs_perf_hooks_module"
501+ ) ;
502+
481503 // Make sure we define an unsafe eval binding and enable the fallback service
482504 runnerWorker . unsafeEvalBinding = "__VITEST_POOL_WORKERS_UNSAFE_EVAL" ;
483505 runnerWorker . unsafeUseModuleFallbackService = true ;
@@ -579,18 +601,18 @@ function buildProjectWorkerOptions(
579601 } ,
580602 // The native workerd provided nodejs modules don't always support everything Vitest needs.
581603 // As a short-term fix, inject polyfills into the worker bundle that override the native modules.
582- {
583- type : "ESModule" ,
584- path : path . join ( modulesRoot , "node:vm" ) ,
585- contents : fs . readFileSync ( path . join ( DIST_PATH , "worker/lib/node/vm.mjs" ) ) ,
586- } ,
587604 {
588605 type : "ESModule" ,
589606 path : path . join ( modulesRoot , "node:console" ) ,
590607 contents : fs . readFileSync (
591- path . join ( DIST_PATH , " worker/lib/ node/console.mjs" )
608+ path . join ( DIST_PATH , ` worker/node/console.mjs` )
592609 ) ,
593610 } ,
611+ {
612+ type : "ESModule" ,
613+ path : path . join ( modulesRoot , "node:vm" ) ,
614+ contents : fs . readFileSync ( path . join ( DIST_PATH , `worker/node/vm.mjs` ) ) ,
615+ } ,
594616 ] ;
595617
596618 // Build array of workers contributed by the workspace
@@ -1241,3 +1263,24 @@ export default function (ctx: Vitest): ProcessPool {
12411263 } ,
12421264 } ;
12431265}
1266+
1267+ /**
1268+ * Ensures that the specified compatibility feature is set correctly.
1269+ * @param flags The list of current flags.
1270+ * @param enableFlag The flag that enables the feature.
1271+ * @param disableFlag The flag that disables the feature.
1272+ */
1273+ function ensureFlag ( flags : string [ ] , enableFlag : string , disableFlag : string ) {
1274+ if ( ! flags . includes ( enableFlag ) ) {
1275+ log . debug (
1276+ `Adding \`${ enableFlag } \` compatibility flag during tests as this feature is needed to support the Vitest runner.`
1277+ ) ;
1278+ flags . push ( enableFlag ) ;
1279+ }
1280+ if ( flags . includes ( disableFlag ) ) {
1281+ log . info (
1282+ `Removing \`${ disableFlag } \` compatibility flag during tests as that feature is needed to support the Vitest runner.`
1283+ ) ;
1284+ flags . splice ( flags . indexOf ( disableFlag ) , 1 ) ;
1285+ }
1286+ }
0 commit comments