11import assert from "node:assert" ;
22import path from "node:path" ;
33import dedent from "ts-dedent" ;
4- import { beforeAll , beforeEach , describe , expect , test , vi } from "vitest" ;
4+ import { afterEach , beforeEach , describe , expect , test , vi } from "vitest" ;
55import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id" ;
6- import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test" ;
6+ import {
7+ importWrangler ,
8+ WranglerE2ETestHelper ,
9+ } from "./helpers/e2e-wrangler-test" ;
710import type { Worker } from "../src/api/startDevWorker" ;
811import type { MockInstance } from "vitest" ;
912
10- type Wrangler = Awaited < ReturnType < WranglerE2ETestHelper [ "importWrangler" ] > > ;
11-
12- describe ( "startWorker - auth options" , ( ) => {
13- let consoleErrorMock : MockInstance < typeof console . error > ;
14-
15- beforeAll ( ( ) => {
16- consoleErrorMock = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
17- } ) ;
13+ const { unstable_startWorker : startWorker } = await importWrangler ( ) ;
1814
15+ describe ( "startWorker - auth options" , { sequential : true } , ( ) => {
16+ let worker : Worker | undefined ;
1917 describe . skipIf ( ! CLOUDFLARE_ACCOUNT_ID ) ( "with remote bindings" , ( ) => {
2018 let helper : WranglerE2ETestHelper ;
21- let wrangler : Wrangler ;
22- let startWorker : Wrangler [ "unstable_startWorker" ] ;
19+
20+ let consoleErrorMock : MockInstance < typeof console . error > ;
2321
2422 beforeEach ( async ( ) => {
23+ consoleErrorMock = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => {
24+ // suppress error output during tests - we are going to check for specific error messages in the tests themselves
25+ } ) ;
26+
2527 helper = new WranglerE2ETestHelper ( ) ;
2628 const aiWorkerScript = dedent `
2729 export default {
@@ -45,13 +47,11 @@ describe("startWorker - auth options", () => {
4547 await helper . seed ( {
4648 "src/index.js" : aiWorkerScript ,
4749 } ) ;
48- wrangler = await helper . importWrangler ( ) ;
49- startWorker = wrangler . unstable_startWorker ;
5050 } ) ;
5151
52- test ( "starting a worker with startWorker with the valid auth information and updating it with invalid information" , async ( t ) => {
53- t . onTestFinished ( async ( ) => await worker ?. dispose ( ) ) ;
52+ afterEach ( ( ) => worker ?. dispose ( ) ) ;
5453
54+ test ( "starting a worker with startWorker with the valid auth information and updating it with invalid information" , async ( ) => {
5555 const validAuth = vi . fn ( ( ) => {
5656 assert ( process . env . CLOUDFLARE_API_TOKEN ) ;
5757
@@ -63,7 +63,7 @@ describe("startWorker - auth options", () => {
6363 } ;
6464 } ) ;
6565
66- const worker = await startWorker ( {
66+ worker = await startWorker ( {
6767 entrypoint : path . resolve ( helper . tmpPath , "src/index.js" ) ,
6868 bindings : {
6969 AI : {
@@ -80,7 +80,7 @@ describe("startWorker - auth options", () => {
8080 } ,
8181 } ) ;
8282
83- await assertValidWorkerAiResponse ( worker ) ;
83+ await assertValidWorkerAiResponse ( ) ;
8484
8585 expect ( validAuth ) . toHaveBeenCalledOnce ( ) ;
8686
@@ -101,14 +101,12 @@ describe("startWorker - auth options", () => {
101101 } ,
102102 } ) ;
103103
104- await assertInvalidWorkerAiResponse ( worker ) ;
104+ await assertInvalidWorkerAiResponse ( ) ;
105105
106106 expect ( incorrectAuth ) . toHaveBeenCalledOnce ( ) ;
107107 } ) ;
108108
109- test ( "starting a worker with startWorker with invalid auth information and updating it with valid auth information" , async ( t ) => {
110- t . onTestFinished ( async ( ) => await worker ?. dispose ( ) ) ;
111-
109+ test ( "starting a worker with startWorker with invalid auth information and updating it with valid auth information" , async ( ) => {
112110 const incorrectAuth = vi . fn ( ( ) => {
113111 return {
114112 accountId : CLOUDFLARE_ACCOUNT_ID ,
@@ -118,7 +116,7 @@ describe("startWorker - auth options", () => {
118116 } ;
119117 } ) ;
120118
121- const worker = await startWorker ( {
119+ worker = await startWorker ( {
122120 entrypoint : path . resolve ( helper . tmpPath , "src/index.js" ) ,
123121 bindings : {
124122 AI : {
@@ -135,7 +133,7 @@ describe("startWorker - auth options", () => {
135133 } ,
136134 } ) ;
137135
138- await assertInvalidWorkerAiResponse ( worker ) ;
136+ await assertInvalidWorkerAiResponse ( ) ;
139137
140138 expect ( incorrectAuth ) . toHaveBeenCalledOnce ( ) ;
141139
@@ -158,12 +156,13 @@ describe("startWorker - auth options", () => {
158156 } ,
159157 } ) ;
160158
161- await assertValidWorkerAiResponse ( worker ) ;
159+ await assertValidWorkerAiResponse ( ) ;
162160
163161 expect ( validAuth ) . toHaveBeenCalledOnce ( ) ;
164162 } ) ;
165163
166- async function assertValidWorkerAiResponse ( worker : Worker ) {
164+ async function assertValidWorkerAiResponse ( ) {
165+ assert ( worker , "Worker is not defined" ) ;
167166 const responseText = await fetchTimedTextFromWorker ( worker ) ;
168167
169168 // We've fixed the auth information so now we can indeed get
@@ -179,7 +178,8 @@ describe("startWorker - auth options", () => {
179178 ) ;
180179 }
181180
182- async function assertInvalidWorkerAiResponse ( worker : Worker ) {
181+ async function assertInvalidWorkerAiResponse ( ) {
182+ assert ( worker , "Worker is not defined" ) ;
183183 const responseText = await fetchTimedTextFromWorker ( worker ) ;
184184
185185 // The remote connection is not established so we can't successfully
@@ -196,12 +196,8 @@ describe("startWorker - auth options", () => {
196196 } ) ;
197197
198198 describe ( "without remote bindings (no auth is needed)" , ( ) => {
199- test ( "starting a worker via startWorker without any remote bindings (doesn't cause wrangler to try to get the auth information)" , async ( t ) => {
200- t . onTestFinished ( async ( ) => await worker ?. dispose ( ) ) ;
201-
199+ test ( "starting a worker via startWorker without any remote bindings (doesn't cause wrangler to try to get the auth information)" , async ( ) => {
202200 const helper = new WranglerE2ETestHelper ( ) ;
203- const wrangler = await helper . importWrangler ( ) ;
204- const startWorker = wrangler . unstable_startWorker ;
205201
206202 const simpleWorkerScript = dedent `
207203 export default {
@@ -223,7 +219,7 @@ describe("startWorker - auth options", () => {
223219 } ;
224220 } ) ;
225221
226- const worker = await startWorker ( {
222+ worker = await startWorker ( {
227223 entrypoint : path . resolve ( helper . tmpPath , "src/index.js" ) ,
228224 dev : {
229225 auth : someAuth ,
@@ -255,6 +251,7 @@ async function fetchTimedTextFromWorker(
255251 let responseText : string | null = null ;
256252
257253 try {
254+ assert ( worker , "Worker is not defined" ) ;
258255 await vi . waitFor (
259256 async ( ) => {
260257 responseText = await (
0 commit comments