1
- import { spawn } from "child_process" ;
2
- import { fetch } from "undici" ;
3
- import type { ChildProcess } from "child_process" ;
4
- import type { Response } from "undici" ;
5
-
6
- const waitUntilReady = async ( url : string ) : Promise < Response > => {
7
- let response : Response | undefined = undefined ;
8
-
9
- while ( response === undefined ) {
10
- await new Promise ( ( resolvePromise ) => setTimeout ( resolvePromise , 100 ) ) ;
11
-
12
- try {
13
- response = await fetch ( url ) ;
14
- } catch { }
15
- }
16
-
17
- return response as Response ;
18
- } ;
19
- const isWindows = process . platform === "win32" ;
20
-
21
- let wranglerProcess : ChildProcess ;
22
-
23
- beforeAll ( async ( ) => {
24
- // These tests break in CI for windows, so we're disabling them for now
25
- if ( isWindows ) return ;
26
-
27
- wranglerProcess = spawn (
28
- "npx" ,
29
- [
30
- "wrangler" ,
31
- "dev" ,
32
- "src/module.ts" ,
33
- "--local" ,
34
- "--config" ,
35
- "src/wrangler.module.toml" ,
36
- "--port" ,
37
- "9001" ,
38
- ] ,
39
- {
40
- shell : isWindows ,
41
- stdio : "inherit" ,
42
- }
43
- ) ;
44
- } ) ;
45
-
46
- afterAll ( async ( ) => {
47
- // These tests break in CI for windows, so we're disabling them for now
48
- if ( isWindows ) return ;
49
-
50
- await new Promise ( ( resolve , reject ) => {
51
- wranglerProcess . once ( "exit" , ( code ) => {
52
- if ( ! code ) {
53
- resolve ( code ) ;
54
- } else {
55
- reject ( code ) ;
56
- }
57
- } ) ;
58
- wranglerProcess . kill ( ) ;
59
- } ) ;
60
- } ) ;
1
+ import { spawnWranglerDev } from "./helpers" ;
61
2
62
3
it ( "renders" , async ( ) => {
63
- // These tests break in CI for windows, so we're disabling them for now
64
- if ( isWindows ) return ;
4
+ const { fetchWhenReady, terminateProcess } = spawnWranglerDev (
5
+ "src/module.ts" ,
6
+ "src/wrangler.module.toml" ,
7
+ 9001
8
+ ) ;
65
9
66
- const response = await waitUntilReady ( "http://localhost:9001/" ) ;
67
- const text = await response . text ( ) ;
68
- expect ( text ) . toMatchInlineSnapshot ( `
10
+ try {
11
+ const response = await fetchWhenReady ( "http://localhost" ) ;
12
+ const text = await response . text ( ) ;
13
+ expect ( text ) . toMatchInlineSnapshot ( `
69
14
"{
70
15
\\"VAR1\\": \\"value1\\",
71
16
\\"VAR2\\": 123,
@@ -76,4 +21,7 @@ it("renders", async () => {
76
21
\\"data\\": \\"Here be some data\\"
77
22
}"
78
23
` ) ;
24
+ } finally {
25
+ await terminateProcess ( ) ;
26
+ }
79
27
} ) ;
0 commit comments