Skip to content

Commit eeaae54

Browse files
ensure that vitest specific nodejs polyfills override native modules
1 parent 9848a75 commit eeaae54

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

.changeset/true-groups-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
ensure that vitest specific nodejs polyfills override native modules

packages/vitest-pool-workers/src/pool/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ const mfLog = new Log(LogLevel.WARN);
120120
const __filename = fileURLToPath(import.meta.url);
121121
const __dirname = path.dirname(__filename);
122122
const DIST_PATH = path.resolve(__dirname, "..");
123-
const POOL_WORKER_PATH = path.join(DIST_PATH, "worker", "index.mjs");
124-
125-
const NODE_URL_PATH = path.join(DIST_PATH, "worker", "lib", "node", "url.mjs");
123+
const POOL_WORKER_PATH = path.join(DIST_PATH, "worker/index.mjs");
126124

127125
const symbolizerWarning =
128126
"warning: Not symbolizing stack traces because $LLVM_SYMBOLIZER is not set.";
@@ -463,6 +461,7 @@ function buildProjectWorkerOptions(
463461
runnerWorker.compatibilityFlags
464462
);
465463

464+
// Force nodejs_compat_v2 flag, even if it is disabled by the user, since we require this native stuff for Vitest to work properly
466465
if (mode !== "v2") {
467466
if (hasNoNodejsCompatV2Flag) {
468467
runnerWorker.compatibilityFlags.splice(
@@ -578,12 +577,26 @@ function buildProjectWorkerOptions(
578577
path: path.join(modulesRoot, DEFINES_MODULE_PATH),
579578
contents: defines,
580579
},
581-
// The workerd provided `node:url` module doesn't support everything Vitest needs.
582-
// As a short-term fix, inject a `node:url` polyfill into the worker bundle
580+
// The native workerd provided nodejs modules don't always support everything Vitest needs.
581+
// As a short-term fix, inject polyfills into the worker bundle that override the native modules.
583582
{
584583
type: "ESModule",
585584
path: path.join(modulesRoot, "node:url"),
586-
contents: fs.readFileSync(NODE_URL_PATH),
585+
contents: fs.readFileSync(
586+
path.join(DIST_PATH, "worker/lib/node/url.mjs")
587+
),
588+
},
589+
{
590+
type: "ESModule",
591+
path: path.join(modulesRoot, "node:vm"),
592+
contents: fs.readFileSync(path.join(DIST_PATH, "worker/lib/node/vm.mjs")),
593+
},
594+
{
595+
type: "ESModule",
596+
path: path.join(modulesRoot, "node:console"),
597+
contents: fs.readFileSync(
598+
path.join(DIST_PATH, "worker/lib/node/console.mjs")
599+
),
587600
},
588601
];
589602

0 commit comments

Comments
 (0)