@@ -28,21 +28,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
28
28
proxyConfig : 'proxy.config.json' ,
29
29
} ) ;
30
30
31
- const proxyServer = createProxyServer ( ) ;
31
+ const proxyServer = await createProxyServer ( ) ;
32
32
try {
33
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
34
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
35
-
36
33
await harness . writeFiles ( {
37
- 'proxy.config.json' : `{ "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
34
+ 'proxy.config.json' : `{ "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
38
35
} ) ;
39
36
40
37
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
41
38
42
39
expect ( result ?. success ) . toBeTrue ( ) ;
43
40
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
44
41
} finally {
45
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
42
+ await proxyServer . close ( ) ;
46
43
}
47
44
} ) ;
48
45
@@ -52,15 +49,12 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
52
49
proxyConfig : 'proxy.config.json' ,
53
50
} ) ;
54
51
55
- const proxyServer = createProxyServer ( ) ;
52
+ const proxyServer = await createProxyServer ( ) ;
56
53
try {
57
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
58
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
59
-
60
54
await harness . writeFiles ( {
61
55
'proxy.config.json' : `
62
56
// JSON file with comments
63
- { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }
57
+ { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
64
58
` ,
65
59
} ) ;
66
60
@@ -69,7 +63,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
69
63
expect ( result ?. success ) . toBeTrue ( ) ;
70
64
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
71
65
} finally {
72
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
66
+ await proxyServer . close ( ) ;
73
67
}
74
68
} ) ;
75
69
@@ -78,22 +72,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
78
72
...BASE_OPTIONS ,
79
73
proxyConfig : 'proxy.config.js' ,
80
74
} ) ;
81
-
82
- const proxyServer = createProxyServer ( ) ;
75
+ const proxyServer = await createProxyServer ( ) ;
83
76
try {
84
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
85
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
86
-
87
77
await harness . writeFiles ( {
88
- 'proxy.config.js' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
78
+ 'proxy.config.js' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
89
79
} ) ;
90
80
91
81
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
92
82
93
83
expect ( result ?. success ) . toBeTrue ( ) ;
94
84
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
95
85
} finally {
96
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
86
+ await proxyServer . close ( ) ;
97
87
}
98
88
} ) ;
99
89
@@ -103,13 +93,10 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
103
93
proxyConfig : 'proxy.config.js' ,
104
94
} ) ;
105
95
106
- const proxyServer = createProxyServer ( ) ;
96
+ const proxyServer = await createProxyServer ( ) ;
107
97
try {
108
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
109
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
110
-
111
98
await harness . writeFiles ( {
112
- 'proxy.config.js' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
99
+ 'proxy.config.js' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
113
100
'package.json' : '{ "type": "module" }' ,
114
101
} ) ;
115
102
@@ -118,7 +105,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
118
105
expect ( result ?. success ) . toBeTrue ( ) ;
119
106
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
120
107
} finally {
121
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
108
+ await proxyServer . close ( ) ;
122
109
}
123
110
} ) ;
124
111
@@ -128,10 +115,9 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
128
115
proxyConfig : 'proxy.config.cjs' ,
129
116
} ) ;
130
117
131
- const proxyServer = createProxyServer ( ) ;
118
+ const proxyServer = await createProxyServer ( ) ;
132
119
try {
133
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
134
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
120
+ const proxyAddress = proxyServer . address ;
135
121
136
122
await harness . writeFiles ( {
137
123
'proxy.config.cjs' : `module.exports = { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
@@ -142,7 +128,7 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
142
128
expect ( result ?. success ) . toBeTrue ( ) ;
143
129
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
144
130
} finally {
145
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
131
+ await proxyServer . close ( ) ;
146
132
}
147
133
} ) ;
148
134
@@ -152,21 +138,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
152
138
proxyConfig : 'proxy.config.mjs' ,
153
139
} ) ;
154
140
155
- const proxyServer = createProxyServer ( ) ;
141
+ const proxyServer = await createProxyServer ( ) ;
156
142
try {
157
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
158
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
159
-
160
143
await harness . writeFiles ( {
161
- 'proxy.config.mjs' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }` ,
144
+ 'proxy.config.mjs' : `export default { "/api/*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }` ,
162
145
} ) ;
163
146
164
147
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
165
148
166
149
expect ( result ?. success ) . toBeTrue ( ) ;
167
150
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
168
151
} finally {
169
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
152
+ await proxyServer . close ( ) ;
170
153
}
171
154
} ) ;
172
155
@@ -176,21 +159,18 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
176
159
proxyConfig : 'proxy.config.json' ,
177
160
} ) ;
178
161
179
- const proxyServer = createProxyServer ( ) ;
162
+ const proxyServer = await createProxyServer ( ) ;
180
163
try {
181
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
182
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
183
-
184
164
await harness . writeFiles ( {
185
- 'proxy.config.json' : `[ { "context": ["/api", "/abc"], "target": "http://127.0.0.1:${ proxyAddress . port } " } ]` ,
165
+ 'proxy.config.json' : `[ { "context": ["/api", "/abc"], "target": "http://127.0.0.1:${ proxyServer . address . port } " } ]` ,
186
166
} ) ;
187
167
188
168
const { result, response } = await executeOnceAndFetch ( harness , '/api/test' ) ;
189
169
190
170
expect ( result ?. success ) . toBeTrue ( ) ;
191
171
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
192
172
} finally {
193
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
173
+ await proxyServer . close ( ) ;
194
174
}
195
175
} ) ;
196
176
@@ -255,14 +235,11 @@ function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
255
235
proxyConfig : 'proxy.config.json' ,
256
236
} ) ;
257
237
258
- const proxyServer = createProxyServer ( ) ;
238
+ const proxyServer = await createProxyServer ( ) ;
259
239
try {
260
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
261
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
262
-
263
240
await harness . writeFiles ( {
264
241
'proxy.config.json' : `
265
- { "^/api/.*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }
242
+ { "^/api/.*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
266
243
` ,
267
244
} ) ;
268
245
@@ -271,7 +248,7 @@ function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
271
248
expect ( result ?. success ) . toBeTrue ( ) ;
272
249
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
273
250
} finally {
274
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
251
+ await proxyServer . close ( ) ;
275
252
}
276
253
} ) ;
277
254
@@ -281,14 +258,11 @@ function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
281
258
proxyConfig : 'proxy.config.json' ,
282
259
} ) ;
283
260
284
- const proxyServer = createProxyServer ( ) ;
261
+ const proxyServer = await createProxyServer ( ) ;
285
262
try {
286
- await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
287
- const proxyAddress = proxyServer . address ( ) as import ( 'net' ) . AddressInfo ;
288
-
289
263
await harness . writeFiles ( {
290
264
'proxy.config.json' : `
291
- { "^\\/(?!something).*": { "target": "http://127.0.0.1:${ proxyAddress . port } " } }
265
+ { "^\\/(?!something).*": { "target": "http://127.0.0.1:${ proxyServer . address . port } " } }
292
266
` ,
293
267
} ) ;
294
268
@@ -297,7 +271,7 @@ function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
297
271
expect ( result ?. success ) . toBeTrue ( ) ;
298
272
expect ( await response ?. text ( ) ) . toContain ( 'TEST_API_RETURN' ) ;
299
273
} finally {
300
- await new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ;
274
+ await proxyServer . close ( ) ;
301
275
}
302
276
} ) ;
303
277
}
@@ -306,11 +280,9 @@ function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
306
280
* Creates an HTTP Server used for proxy testing that provides a `/test` endpoint
307
281
* that returns a 200 response with a body of `TEST_API_RETURN`. All other requests
308
282
* will return a 404 response.
309
- *
310
- * @returns An HTTP Server instance.
311
283
*/
312
- function createProxyServer ( ) {
313
- return createServer ( ( request , response ) => {
284
+ async function createProxyServer ( ) {
285
+ const proxyServer = createServer ( ( request , response ) => {
314
286
if ( request . url ?. endsWith ( '/test' ) ) {
315
287
response . writeHead ( 200 ) ;
316
288
response . end ( 'TEST_API_RETURN' ) ;
@@ -319,4 +291,11 @@ function createProxyServer() {
319
291
response . end ( ) ;
320
292
}
321
293
} ) ;
294
+
295
+ await new Promise < void > ( ( resolve ) => proxyServer . listen ( 0 , '127.0.0.1' , resolve ) ) ;
296
+
297
+ return {
298
+ address : proxyServer . address ( ) as import ( 'net' ) . AddressInfo ,
299
+ close : ( ) => new Promise < void > ( ( resolve ) => proxyServer . close ( ( ) => resolve ( ) ) ) ,
300
+ } ;
322
301
}
0 commit comments