Skip to content

Commit 3c770ec

Browse files
fix the Vitest integration to support Workers that want Node.js compatibility is only v1 in production (#9331)
It does this by adding the `nodejs_compat_v2` flag (if missing) and removing `no_nodejs_compat_v2` flag (if found). This does mean that the Vitest tests are running with a slightly different environment to production, but this has always been the case in other ways. Fixes #8988
1 parent b3f290a commit 3c770ec

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

.changeset/shy-beans-warn.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
fix the Vitest integration to support Workers that want Node.js compatibility is only v1 in production
6+
7+
It does this by adding the `nodejs_compat_v2` flag (if missing) and removing `no_nodejs_compat_v2` flag (if found).
8+
9+
This does mean that the Vitest tests are running with a slightly different environment to production, but this has always been the case in other ways.

fixtures/vitest-pool-workers-examples/basics-integration-auxiliary/vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ export default defineWorkersProject({
1010
// Configuration for the test runner Worker
1111
compatibilityDate: "2024-01-01",
1212
compatibilityFlags: [
13+
// This illustrates a Worker that in production only wants v1 of Node.js compatibility.
14+
// The Vitest pool integration will need to remove this flag since the `MockAgent` requires v2.
15+
"no_nodejs_compat_v2",
1316
"nodejs_compat",
1417
// Required to use `WORKER.scheduled()`. This is an experimental
1518
// compatibility flag, and cannot be enabled in production.

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,18 @@ function buildProjectWorkerOptions(
407407
}
408408
}
409409

410-
const { mode } = getNodeCompat(
410+
const { hasNoNodejsCompatV2Flag, mode } = getNodeCompat(
411411
runnerWorker.compatibilityDate,
412412
runnerWorker.compatibilityFlags
413413
);
414414

415415
if (mode !== "v2") {
416+
if (hasNoNodejsCompatV2Flag) {
417+
runnerWorker.compatibilityFlags.splice(
418+
runnerWorker.compatibilityFlags.indexOf("no_nodejs_compat_v2"),
419+
1
420+
);
421+
}
416422
runnerWorker.compatibilityFlags.push("nodejs_compat_v2");
417423
}
418424

0 commit comments

Comments
 (0)