@@ -23,6 +23,7 @@ import {
2323import semverSatisfies from "semver/functions/satisfies.js" ;
2424import { createMethodsRPC } from "vitest/node" ;
2525import { createChunkingSocket } from "../shared/chunking-socket" ;
26+ import { CompatibilityFlagAssertions } from "./compatibility-flag-assertions" ;
2627import { OPTIONS_PATH , parseProjectOptions } from "./config" ;
2728import {
2829 getProjectPath ,
@@ -319,68 +320,6 @@ const SELF_SERVICE_BINDING = "__VITEST_POOL_WORKERS_SELF_SERVICE";
319320const LOOPBACK_SERVICE_BINDING = "__VITEST_POOL_WORKERS_LOOPBACK_SERVICE" ;
320321const RUNNER_OBJECT_BINDING = "__VITEST_POOL_WORKERS_RUNNER_OBJECT" ;
321322
322- const numericCompare = new Intl . Collator ( "en" , { numeric : true } ) . compare ;
323-
324- interface CompatibilityFlagCheckOptions {
325- // Context to check against
326- compatibilityFlags : string [ ] ;
327- compatibilityDate ?: string ;
328- relativeProjectPath : string | number ;
329- relativeWranglerConfigPath ?: string ;
330-
331- // Details on flag to check
332- enableFlag : string ;
333- disableFlag ?: string ;
334- defaultOnDate ?: string ;
335- }
336- function assertCompatibilityFlagEnabled ( opts : CompatibilityFlagCheckOptions ) {
337- const hasWranglerConfig = opts . relativeWranglerConfigPath !== undefined ;
338-
339- // Check disable flag (if any) not enabled
340- if (
341- opts . disableFlag !== undefined &&
342- opts . compatibilityFlags . includes ( opts . disableFlag )
343- ) {
344- let message = `In project ${ opts . relativeProjectPath } ` ;
345- if ( hasWranglerConfig ) {
346- message += `'s configuration file ${ opts . relativeWranglerConfigPath } , \`compatibility_flags\` must not contain "${ opts . disableFlag } ".\nSimilarly` ;
347- // Since the config is merged by this point, we don't know where the
348- // disable flag came from. So we include both possible locations in the
349- // error message. Note the enable-flag case doesn't have this problem, as
350- // we're asking the user to add something to *either* of their configs.
351- }
352- message +=
353- `, \`${ OPTIONS_PATH } .miniflare.compatibilityFlags\` must not contain "${ opts . disableFlag } ".\n` +
354- "This flag is incompatible with `@cloudflare/vitest-pool-workers`." ;
355- throw new Error ( message ) ;
356- }
357-
358- // Check flag enabled or compatibility date enables flag by default
359- const enabledByFlag = opts . compatibilityFlags . includes ( opts . enableFlag ) ;
360- const enabledByDate =
361- opts . compatibilityDate !== undefined &&
362- opts . defaultOnDate !== undefined &&
363- numericCompare ( opts . compatibilityDate , opts . defaultOnDate ) >= 0 ;
364- if ( ! ( enabledByFlag || enabledByDate ) ) {
365- let message = `In project ${ opts . relativeProjectPath } ` ;
366- if ( hasWranglerConfig ) {
367- message += `'s configuration file ${ opts . relativeWranglerConfigPath } , \`compatibility_flags\` must contain "${ opts . enableFlag } "` ;
368- } else {
369- message += `, \`${ OPTIONS_PATH } .miniflare.compatibilityFlags\` must contain "${ opts . enableFlag } "` ;
370- }
371- if ( opts . defaultOnDate !== undefined ) {
372- if ( hasWranglerConfig ) {
373- message += `, or \`compatibility_date\` must be >= "${ opts . defaultOnDate } "` ;
374- } else {
375- message += `, or \`${ OPTIONS_PATH } .miniflare.compatibilityDate\` must be >= "${ opts . defaultOnDate } "` ;
376- }
377- }
378- message +=
379- ".\nThis flag is required to use `@cloudflare/vitest-pool-workers`." ;
380- throw new Error ( message ) ;
381- }
382- }
383-
384323function buildProjectWorkerOptions (
385324 project : Omit < Project , "testFiles" >
386325) : ProjectWorkers {
@@ -400,24 +339,36 @@ function buildProjectWorkerOptions(
400339 // of the libraries it depends on expect `require()` to return
401340 // `module.exports` directly, rather than `{ default: module.exports }`.
402341 runnerWorker . compatibilityFlags ??= [ ] ;
403- assertCompatibilityFlagEnabled ( {
404- compatibilityFlags : runnerWorker . compatibilityFlags ,
342+
343+ const flagAssertions = new CompatibilityFlagAssertions ( {
405344 compatibilityDate : runnerWorker . compatibilityDate ,
406- relativeProjectPath : project . relativePath ,
407- relativeWranglerConfigPath,
408- // https://developers.cloudflare.com/workers/configuration/compatibility-dates/#commonjs-modules-do-not-export-a-module-namespace
409- enableFlag : "export_commonjs_default" ,
410- disableFlag : "export_commonjs_namespace" ,
411- defaultOnDate : "2022-10-31" ,
412- } ) ;
413- assertCompatibilityFlagEnabled ( {
414345 compatibilityFlags : runnerWorker . compatibilityFlags ,
415- compatibilityDate : runnerWorker . compatibilityDate ,
416- relativeProjectPath : project . relativePath ,
346+ optionsPath : ` ${ OPTIONS_PATH } .miniflare` ,
347+ relativeProjectPath : project . relativePath . toString ( ) ,
417348 relativeWranglerConfigPath,
418- enableFlag : "nodejs_compat" ,
419349 } ) ;
420350
351+ const assertions = [
352+ ( ) =>
353+ flagAssertions . assertIsEnabled ( {
354+ enableFlag : "export_commonjs_default" ,
355+ disableFlag : "export_commonjs_namespace" ,
356+ defaultOnDate : "2022-10-31" ,
357+ } ) ,
358+ ( ) =>
359+ flagAssertions . assertAtLeastOneFlagExists ( [
360+ "nodejs_compat" ,
361+ "nodejs_compat_v2" ,
362+ ] ) ,
363+ ] ;
364+
365+ for ( const assertion of assertions ) {
366+ const result = assertion ( ) ;
367+ if ( ! result . isValid ) {
368+ throw new Error ( result . errorMessage ) ;
369+ }
370+ }
371+
421372 // Required for `workerd:unsafe` module. We don't require this flag to be set
422373 // as it's experimental, so couldn't be deployed by users.
423374 if ( ! runnerWorker . compatibilityFlags . includes ( "unsafe_module" ) ) {
0 commit comments