@@ -12,6 +12,7 @@ import {
1212import { detectPackageManager } from "helpers/packageManagers" ;
1313import { retry } from "helpers/retry" ;
1414import { sleep } from "helpers/sleep" ;
15+ import * as jsonc from "jsonc-parser" ;
1516import { fetch } from "undici" ;
1617import { beforeAll , describe , expect } from "vitest" ;
1718import { deleteProject , deleteWorker } from "../scripts/common" ;
@@ -41,12 +42,9 @@ import type { Writable } from "stream";
4142
4243type FrameworkTestConfig = RunnerConfig & {
4344 testCommitMessage : boolean ;
45+ nodeCompat : boolean ;
4446 unsupportedPms ?: string [ ] ;
4547 unsupportedOSs ?: string [ ] ;
46- verifyBuildCfTypes ?: {
47- outputFile : string ;
48- envInterfaceName : string ;
49- } ;
5048 verifyBuild ?: {
5149 outputDir : string ;
5250 script : string ;
@@ -80,8 +78,12 @@ describe.concurrent(
8078 } ) ;
8179
8280 Object . entries ( frameworkTests ) . forEach ( ( [ frameworkKey , testConfig ] ) => {
83- const frameworkConfig = getFrameworkConfig ( frameworkKey ) ;
84-
81+ const frameworkConfig = {
82+ workersTypes : "generated" as const ,
83+ typesPath : "worker-configuration.d.ts" ,
84+ envInterfaceName : "Env" ,
85+ ...getFrameworkConfig ( frameworkKey ) ,
86+ } ;
8587 test ( { experimental } ) . runIf (
8688 shouldRunTest ( frameworkConfig . id , testConfig ) ,
8789 ) (
@@ -176,7 +178,8 @@ describe.concurrent(
176178 project . path ,
177179 logStream ,
178180 ) ;
179- await verifyBuildCfTypesScript ( testConfig , project . path , logStream ) ;
181+
182+ await verifyTypes ( testConfig , frameworkConfig , project . path ) ;
180183 await verifyBuildScript ( testConfig , project . path , logStream ) ;
181184 } catch ( e ) {
182185 console . error ( "ERROR" , e ) ;
@@ -352,57 +355,53 @@ const verifyPreviewScript = async (
352355 }
353356} ;
354357
355- const verifyBuildCfTypesScript = async (
356- { verifyBuildCfTypes } : FrameworkTestConfig ,
358+ const verifyTypes = async (
359+ { nodeCompat } : FrameworkTestConfig ,
360+ {
361+ workersTypes,
362+ typesPath = "./worker-configuration.d.ts" ,
363+ envInterfaceName = "Env" ,
364+ } : TemplateConfig ,
357365 projectPath : string ,
358- logStream : Writable ,
359366) => {
360- if ( ! verifyBuildCfTypes ) {
367+ if ( workersTypes === "none" ) {
361368 return ;
362369 }
363370
364- const { outputFile, envInterfaceName } = verifyBuildCfTypes ;
365-
366- const outputFileContentPre = readFile ( join ( projectPath , outputFile ) ) ;
367- const outputFileContentPreLines = outputFileContentPre . split ( "\n" ) ;
371+ const outputFileContent = readFile ( join ( projectPath , typesPath ) ) . split ( "\n" ) ;
368372
369- // the file contains the "Generated by Wrangler" comment without a timestamp
370- expect ( outputFileContentPreLines ) . toContain ( "// Generated by Wrangler" ) ;
371-
372- // the file contains the env interface
373- // the file still contains the env interface
374- const hasEnvInterfacePre = outputFileContentPreLines . some (
373+ const hasEnvInterface = outputFileContent . some (
375374 ( line ) =>
376375 // old type gen - some framework templates pin older versions of wrangler
377376 line === `interface ${ envInterfaceName } {` ||
378377 // new after importable env change
379378 line === `interface ${ envInterfaceName } extends Cloudflare.Env {}` ,
380379 ) ;
381- expect ( hasEnvInterfacePre ) . toBe ( true ) ;
382-
383- // Run the `cf-typegen` script to generate types for bindings in fixture
384- const buildTypesProc = spawnWithLogging (
385- [ pm , "run" , "cf-typegen" ] ,
386- { cwd : projectPath } ,
387- logStream ,
388- ) ;
389- await waitForExit ( buildTypesProc ) ;
390-
391- const outputFileContentPost = readFile ( join ( projectPath , outputFile ) ) ;
392- const outputFileContentPostLines = outputFileContentPost . split ( "\n" ) ;
380+ expect ( hasEnvInterface ) . toBe ( true ) ;
393381
394- // the file doesn't contain the "Generated by Wrangler" comment anymore
395- expect ( outputFileContentPostLines ) . not . toContain ( "// Generated by Wrangler" ) ;
382+ // if the runtime types were installed, they wont be in this file
383+ if ( workersTypes === "generated" ) {
384+ expect ( outputFileContent [ 2 ] ) . match (
385+ / \/ \/ R u n t i m e t y p e s g e n e r a t e d w i t h w o r k e r d @ 1 \. \d { 8 } \. \d \d { 4 } - \d { 2 } - \d { 2 } ( [ a - z _ ] + , ? ) * / ,
386+ ) ;
387+ }
396388
397- // the file still contains the env interface
398- const hasEnvInterfacePost = outputFileContentPostLines . some (
399- ( line ) =>
400- // old type gen - some framework templates pin older versions of wrangler
401- line === `interface ${ envInterfaceName } {` ||
402- // new after importable env change
403- line === `interface ${ envInterfaceName } extends Cloudflare.Env {}` ,
404- ) ;
405- expect ( hasEnvInterfacePost ) . toBe ( true ) ;
389+ const tsconfigPath = join ( projectPath , "tsconfig.json" ) ;
390+ const tsconfigTypes = jsonc . parse ( readFile ( tsconfigPath ) ) . compilerOptions
391+ ?. types ;
392+ if ( workersTypes === "generated" ) {
393+ expect ( tsconfigTypes ) . toContain ( typesPath ) ;
394+ }
395+ if ( workersTypes === "installed" ) {
396+ expect (
397+ tsconfigTypes . some ( ( x : string ) =>
398+ x . includes ( "@cloudflare/workers-types" ) ,
399+ ) ,
400+ ) . toBe ( true ) ;
401+ }
402+ if ( nodeCompat ) {
403+ expect ( tsconfigTypes ) . toContain ( `node` ) ;
404+ }
406405} ;
407406
408407const verifyBuildScript = async (
0 commit comments