11import assert from "node:assert" ;
22import dedent from "ts-dedent" ;
33import { fetch } from "undici" ;
4- import { afterAll , describe , expect , it , vi } from "vitest" ;
4+ import { afterAll , beforeAll , describe , expect , it , vi } from "vitest" ;
55import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id" ;
66import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test" ;
77import { generateResourceName } from "./helpers/generate-resource-name" ;
@@ -14,6 +14,8 @@ const normalize = (str: string) =>
1414 [ CLOUDFLARE_ACCOUNT_ID ] : "CLOUDFLARE_ACCOUNT_ID" ,
1515 } ) . replaceAll ( / ^ A u t h o r : ( \s + ) .+ @ .+ $ / gm, "Author:[email protected] " ) ; 1616const workerName = generateResourceName ( ) ;
17+ const dispatchNamespaceName = generateResourceName ( "dispatch" ) ;
18+ const dispatchWorkerName = generateResourceName ( ) ;
1719
1820describe ( "deployments" , { timeout : TIMEOUT } , ( ) => {
1921 let deployedUrl : string ;
@@ -250,11 +252,118 @@ const checkAssets = async (testCases: AssetTestCase[], deployedUrl: string) => {
250252 }
251253} ;
252254
253- describe ( "Workers + Assets deployment" , { timeout : TIMEOUT } , ( ) => {
254- let deployedUrl : string ;
255+ describe . each ( [
256+ {
257+ name : "regular Worker" ,
258+ flags : "" ,
259+ async beforeAll ( ) { } ,
260+ async afterAll ( helper : WranglerE2ETestHelper ) {
261+ await helper . run ( `wrangler delete` ) ;
262+ } ,
263+ expectInitialStdout : ( output : string ) => {
264+ expect ( output ) . toEqual ( `🌀 Building list of assets...
265+ 🌀 Starting asset upload...
266+ 🌀 Found 3 new or modified static assets to upload. Proceeding with upload...
267+ + /404.html
268+ + /index.html
269+ + /[boop].html
270+ Uploaded 1 of 3 assets
271+ Uploaded 2 of 3 assets
272+ Uploaded 3 of 3 assets
273+ ✨ Success! Uploaded 3 files (TIMINGS)
274+ Total Upload: xx KiB / gzip: xx KiB
275+ Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
276+ Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
277+ https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
278+ Current Version ID: 00000000-0000-0000-0000-000000000000` ) ;
279+ } ,
280+ expectSubsequentStdout : ( output : string ) => {
281+ expect ( output ) . toEqual ( `🌀 Building list of assets...
282+ 🌀 Starting asset upload...
283+ No files to upload. Proceeding with deployment...
284+ Total Upload: xx KiB / gzip: xx KiB
285+ Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
286+ Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
287+ https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
288+ Current Version ID: 00000000-0000-0000-0000-000000000000` ) ;
289+ } ,
290+ } ,
291+ {
292+ name : "Workers for Platforms" ,
293+ flags : `--dispatch-namespace ${ dispatchNamespaceName } ` ,
294+ url : "" ,
295+ async beforeAll ( helper : WranglerE2ETestHelper ) {
296+ await helper . seed ( {
297+ "dispatch-worker/wrangler.toml" : dedent `
298+ name = "${ dispatchWorkerName } "
299+ main = "./src/index.js"
300+ compatibility_date = "2023-01-01"
301+
302+ [[dispatch_namespaces]]
303+ binding = "DISPATCH"
304+ namespace = "${ dispatchNamespaceName } "
305+ ` ,
306+ "dispatch-worker/src/index.js" : dedent `
307+ export default {
308+ async fetch(request, env, ctx) {
309+ const stub = env.DISPATCH.get("${ workerName } ");
310+ return stub.fetch(request);
311+ }
312+ }
313+ ` ,
314+ } ) ;
315+ await helper . run (
316+ `wrangler dispatch-namespace create ${ dispatchNamespaceName } `
317+ ) ;
318+ const { stdout } = await helper . run (
319+ `wrangler deploy -c dispatch-worker/wrangler.toml`
320+ ) ;
321+ const match = stdout . match (
322+ / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
323+ ) ;
324+ assert ( match ?. groups ) ;
325+ this . url = match . groups . url ;
326+ } ,
327+ async afterAll ( helper : WranglerE2ETestHelper ) {
328+ await helper . run ( `wrangler delete -c dispatch-worker/wrangler.toml` ) ;
329+ await helper . run (
330+ `wrangler dispatch-namespace delete ${ dispatchNamespaceName } `
331+ ) ;
332+ } ,
333+ expectInitialStdout : ( output : string ) => {
334+ expect ( output ) . toEqual ( `🌀 Building list of assets...
335+ 🌀 Starting asset upload...
336+ 🌀 Found 3 new or modified static assets to upload. Proceeding with upload...
337+ + /404.html
338+ + /index.html
339+ + /[boop].html
340+ Uploaded 1 of 3 assets
341+ Uploaded 2 of 3 assets
342+ Uploaded 3 of 3 assets
343+ ✨ Success! Uploaded 3 files (TIMINGS)
344+ Total Upload: xx KiB / gzip: xx KiB
345+ Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
346+ Dispatch Namespace: tmp-e2e-dispatch-00000000-0000-0000-0000-000000000000
347+ Current Version ID: 00000000-0000-0000-0000-000000000000` ) ;
348+ } ,
349+ expectSubsequentStdout : ( output : string ) => {
350+ expect ( output ) . toEqual ( `🌀 Building list of assets...
351+ 🌀 Starting asset upload...
352+ No files to upload. Proceeding with deployment...
353+ Total Upload: xx KiB / gzip: xx KiB
354+ Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
355+ Dispatch Namespace: tmp-e2e-dispatch-00000000-0000-0000-0000-000000000000
356+ Current Version ID: 00000000-0000-0000-0000-000000000000` ) ;
357+ } ,
358+ } ,
359+ ] ) ( "Workers + Assets deployment: $name" , { timeout : TIMEOUT } , ( testcase ) => {
360+ let deployedUrl : string | undefined ;
255361 const helper = new WranglerE2ETestHelper ( ) ;
362+ beforeAll ( async ( ) => {
363+ await testcase . beforeAll ( helper ) ;
364+ } ) ;
256365 afterAll ( async ( ) => {
257- await helper . run ( `wrangler delete` ) ;
366+ await testcase . afterAll ( helper ) ;
258367 } ) ;
259368 it ( "deploys a Workers + Assets project with assets only" , async ( ) => {
260369 await helper . seed ( {
@@ -267,29 +376,17 @@ describe("Workers + Assets deployment", { timeout: TIMEOUT }, () => {
267376 ...initialAssets ,
268377 } ) ;
269378
270- const output = await helper . run ( `wrangler deploy` ) ;
271- expect ( normalize ( output . stdout ) ) . toMatchInlineSnapshot ( `
272- "🌀 Building list of assets...
273- 🌀 Starting asset upload...
274- 🌀 Found 3 new or modified static assets to upload. Proceeding with upload...
275- + /404.html
276- + /index.html
277- + /[boop].html
278- Uploaded 1 of 3 assets
279- Uploaded 2 of 3 assets
280- Uploaded 3 of 3 assets
281- ✨ Success! Uploaded 3 files (TIMINGS)
282- Total Upload: xx KiB / gzip: xx KiB
283- Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
284- Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
285- https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
286- Current Version ID: 00000000-0000-0000-0000-000000000000"
287- ` ) ;
288- const match = output . stdout . match (
289- / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
290- ) ;
291- assert ( match ?. groups ) ;
292- deployedUrl = match . groups . url ;
379+ const output = await helper . run ( `wrangler deploy ${ testcase . flags } ` ) ;
380+ testcase . expectInitialStdout ( normalize ( output . stdout ) ) ;
381+ if ( testcase . url ) {
382+ deployedUrl = testcase . url ;
383+ } else {
384+ const match = output . stdout . match (
385+ / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
386+ ) ;
387+ assert ( match ?. groups ) ;
388+ deployedUrl = match . groups . url ;
389+ }
293390
294391 const testCases : AssetTestCase [ ] = [
295392 // Tests html_handling = "auto_trailing_slash" (default):
@@ -349,23 +446,16 @@ describe("Workers + Assets deployment", { timeout: TIMEOUT }, () => {
349446 }` ,
350447 ...initialAssets ,
351448 } ) ;
352- const output = await helper . run ( `wrangler deploy` ) ;
449+ const output = await helper . run ( `wrangler deploy ${ testcase . flags } ` ) ;
353450 // expect only no asset files to be uploaded as no new asset files have been added
354- expect ( normalize ( output . stdout ) ) . toMatchInlineSnapshot ( `
355- "🌀 Building list of assets...
356- 🌀 Starting asset upload...
357- No files to upload. Proceeding with deployment...
358- Total Upload: xx KiB / gzip: xx KiB
359- Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
360- Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
361- https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
362- Current Version ID: 00000000-0000-0000-0000-000000000000"
363- ` ) ;
364- const match = output . stdout . match (
365- / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
366- ) ;
367- assert ( match ?. groups ) ;
368- deployedUrl = match . groups . url ;
451+ testcase . expectSubsequentStdout ( normalize ( output . stdout ) ) ;
452+ if ( ! deployedUrl ) {
453+ const match = output . stdout . match (
454+ / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
455+ ) ;
456+ assert ( match ?. groups ) ;
457+ deployedUrl = match . groups . url ;
458+ }
369459
370460 const testCases : AssetTestCase [ ] = [
371461 // because html handling has now been set to "none", only exact matches will be served
@@ -400,7 +490,6 @@ describe("Workers + Assets deployment", { timeout: TIMEOUT }, () => {
400490 ) ;
401491 expect ( text ) . toContain ( "<h1>404.html</h1>" ) ;
402492 } ) ;
403-
404493 it ( "runs user worker ahead of matching assets when serve_directly = false" , async ( ) => {
405494 await helper . seed ( {
406495 "wrangler.toml" : dedent `
@@ -423,24 +512,16 @@ describe("Workers + Assets deployment", { timeout: TIMEOUT }, () => {
423512 ...initialAssets ,
424513 } ) ;
425514
426- const output = await helper . run ( `wrangler deploy` ) ;
515+ const output = await helper . run ( `wrangler deploy ${ testcase . flags } ` ) ;
427516 // expect only no asset files to be uploaded as no new asset files have been added
428- expect ( normalize ( output . stdout ) ) . toMatchInlineSnapshot ( `
429- "🌀 Building list of assets...
430- 🌀 Starting asset upload...
431- No files to upload. Proceeding with deployment...
432- Total Upload: xx KiB / gzip: xx KiB
433- Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
434- Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
435- https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
436- Current Version ID: 00000000-0000-0000-0000-000000000000"
437- ` ) ;
438-
439- const match = output . stdout . match (
440- / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
441- ) ;
442- assert ( match ?. groups ) ;
443- deployedUrl = match . groups . url ;
517+ testcase . expectSubsequentStdout ( normalize ( output . stdout ) ) ;
518+ if ( ! deployedUrl ) {
519+ const match = output . stdout . match (
520+ / (?< url > h t t p s : \/ \/ t m p - e 2 e - .+ ?\. .+ ?\. w o r k e r s \. d e v ) /
521+ ) ;
522+ assert ( match ?. groups ) ;
523+ deployedUrl = match . groups . url ;
524+ }
444525
445526 const testCases : AssetTestCase [ ] = [
446527 {
0 commit comments