11import childProcess from "node:child_process" ;
22import { existsSync } from "node:fs" ;
33import fs from "node:fs/promises" ;
4- import os from "node:os" ;
4+ import os , { tmpdir } from "node:os" ;
55import path from "node:path" ;
66import { setTimeout } from "node:timers/promises" ;
77import { removeDir } from "@fixture/shared/src/fs-helpers" ;
88import { fetch } from "undici" ;
99import { afterAll , assert , beforeAll , describe , test } from "vitest" ;
10- import {
11- runWranglerDev ,
12- wranglerEntryPath ,
13- } from "../../shared/src/run-wrangler-long-lived" ;
10+ import { createServer , type WorkerServer } from "wrangler" ;
11+ import { wranglerEntryPath } from "../../shared/src/run-wrangler-long-lived" ;
1412
1513async function getTmpDir ( ) {
1614 return fs . mkdtemp ( path . join ( os . tmpdir ( ) , "wrangler-modules-" ) ) ;
1715}
1816
19- type WranglerDev = Awaited < ReturnType < typeof runWranglerDev > > ;
20- function get ( worker : WranglerDev , pathname : string ) {
21- const url = `http://${ worker . ip } :${ worker . port } ${ pathname } ` ;
17+ function get ( server : WorkerServer , pathname : string ) {
2218 // Disable Miniflare's pretty error page, so we can parse errors as JSON
23- return fetch ( url , { headers : { "MF-Disable-Pretty-Error" : "true" } } ) ;
19+ return server . fetch ( pathname , {
20+ headers : { "MF-Disable-Pretty-Error" : "true" } ,
21+ } ) ;
2422}
2523
2624async function retry < T > ( closure : ( ) => Promise < T > , max = 30 ) : Promise < T > {
@@ -37,7 +35,8 @@ async function retry<T>(closure: () => Promise<T>, max = 30): Promise<T> {
3735
3836describe ( "wildcard imports: dev" , ( ) => {
3937 let tmpDir : string ;
40- let worker : WranglerDev ;
38+ let server : WorkerServer ;
39+ let url : URL ;
4140
4241 beforeAll ( async ( ) => {
4342 // Copy over files to a temporary directory as we'll be modifying them
@@ -52,31 +51,36 @@ describe("wildcard imports: dev", () => {
5251 path . join ( tmpDir , "wrangler.jsonc" )
5352 ) ;
5453
55- worker = await runWranglerDev ( tmpDir , [ "--port=0" , "--inspector-port=0" ] ) ;
54+ server = createServer ( {
55+ root : tmpDir ,
56+ workers : [ { configPath : "wrangler.jsonc" } ] ,
57+ watch : true ,
58+ } ) ;
59+ ( { url } = await server . listen ( ) ) ;
5660 } ) ;
5761 afterAll ( async ( ) => {
58- await worker . stop ( ) ;
62+ await server . close ( ) ;
5963 removeDir ( tmpDir , { fireAndForget : true } ) ;
6064 } ) ;
6165
6266 test ( "supports bundled modules" , async ( { expect } ) => {
63- const res = await get ( worker , "/dep" ) ;
67+ const res = await get ( server , "/dep" ) ;
6468 expect ( await res . text ( ) ) . toBe ( "bundled" ) ;
6569 } ) ;
6670 test ( "supports text modules" , async ( { expect } ) => {
67- const res = await get ( worker , "/text" ) ;
71+ const res = await get ( server , "/text" ) ;
6872 expect ( await res . text ( ) ) . toBe ( "test\n" ) ;
6973 } ) ;
7074 test ( "supports dynamic imports" , async ( { expect } ) => {
71- const res = await get ( worker , "/dynamic" ) ;
75+ const res = await get ( server , "/dynamic" ) ;
7276 expect ( await res . text ( ) ) . toBe ( "dynamic" ) ;
7377 } ) ;
7478 test ( "supports commonjs lazy imports" , async ( { expect } ) => {
75- const res = await get ( worker , "/common" ) ;
79+ const res = await get ( server , "/common" ) ;
7680 expect ( await res . text ( ) ) . toBe ( "common" ) ;
7781 } ) ;
7882 test ( "supports variable dynamic imports" , async ( { expect } ) => {
79- const res = await get ( worker , "/lang/en" ) ;
83+ const res = await get ( server , "/lang/en" ) ;
8084 expect ( await res . text ( ) ) . toBe ( "hello" ) ;
8185 } ) ;
8286
@@ -89,14 +93,16 @@ describe("wildcard imports: dev", () => {
8993 'export default "new dynamic";'
9094 ) ;
9195 await retry ( async ( ) => {
92- const res = await get ( worker , "/dynamic" ) ;
96+ const res = await get ( server , "/dynamic" ) ;
9397 assert . strictEqual ( await res . text ( ) , "new dynamic" ) ;
9498 } ) ;
9599
96100 // Delete dynamically imported file
97101 await fs . rm ( path . join ( srcDir , "lang" , "en.js" ) ) ;
98102 const res = await retry ( async ( ) => {
99- const res = await get ( worker , "/lang/en" ) ;
103+ const res = await fetch ( new URL ( "/lang/en" , url ) , {
104+ headers : { "MF-Disable-Pretty-Error" : "true" } ,
105+ } ) ;
100106 assert . strictEqual ( res . status , 500 ) ;
101107 return res ;
102108 } ) ;
@@ -110,7 +116,7 @@ describe("wildcard imports: dev", () => {
110116 'export default { hello: "hey" };'
111117 ) ;
112118 await retry ( async ( ) => {
113- const res = await get ( worker , "/lang/en/us" ) ;
119+ const res = await get ( server , "/lang/en/us" ) ;
114120 assert . strictEqual ( await res . text ( ) , "hey" ) ;
115121 } ) ;
116122
@@ -120,7 +126,7 @@ describe("wildcard imports: dev", () => {
120126 'export default { hello: "bye" };'
121127 ) ;
122128 await retry ( async ( ) => {
123- const res = await get ( worker , "/lang/en/us" ) ;
129+ const res = await get ( server , "/lang/en/us" ) ;
124130 assert . strictEqual ( await res . text ( ) , "bye" ) ;
125131 } ) ;
126132 } ) ;
0 commit comments