11import { once } from "events" ;
22import path from "path" ;
33import dedent from "ts-dedent" ;
4- import { test as base , describe } from "vitest" ;
4+ import { describe , test } from "vitest" ;
55import { BundlerController } from "../../../api/startDevWorker/BundlerController" ;
66import { mockConsoleMethods } from "../../helpers/mock-console" ;
77import { runInTempDir } from "../../helpers/run-in-tmp" ;
@@ -16,17 +16,6 @@ function findSourceFile(source: string, name: string): string {
1616 return source . slice ( startIndex , endIndex ) ;
1717}
1818
19- const test = base . extend < { controller : BundlerController } > ( {
20- // eslint-disable-next-line no-empty-pattern
21- controller : async ( { } , use ) => {
22- const controller = new BundlerController ( ) ;
23-
24- await use ( controller ) ;
25-
26- await controller . teardown ( ) ;
27- } ,
28- } ) ;
29-
3019async function waitForBundleComplete (
3120 controller : BundlerController
3221) : Promise < BundleCompleteEvent > {
@@ -54,8 +43,22 @@ describe("BundleController", () => {
5443 mockConsoleMethods ( ) ;
5544 runInTempDir ( ) ;
5645
46+ // We are not using `test.extend` or `onTestFinished` helpers here to create and tear down
47+ // the controller because these run the teardown after all the `afterEach()` blocks have run.
48+ // This means that the controller doesn't get torn down until after the temporary directory has been
49+ // removed.
50+ // And so the file watchers that the controller creates can randomly fail because they are trying to
51+ // watch files in a directory that no longer exists.
52+ // By doing it ourselves in `beforeEach()` and `afterEach()` we can ensure the controller
53+ // is torn down before the temporary directory is removed.
54+ let controller : BundlerController ;
55+ beforeEach ( ( ) => {
56+ controller = new BundlerController ( ) ;
57+ } ) ;
58+ afterEach ( ( ) => controller . teardown ( ) ) ;
59+
5760 describe ( "happy path bundle + watch" , ( ) => {
58- test ( "single ts source file" , async ( { controller } ) => {
61+ test ( "single ts source file" , async ( ) => {
5962 await seed ( {
6063 "src/index.ts" : dedent /* javascript */ `
6164 export default {
@@ -132,7 +135,7 @@ describe("BundleController", () => {
132135 ` ) ;
133136 } ) ;
134137
135- test ( "multiple ts source files" , async ( { controller } ) => {
138+ test ( "multiple ts source files" , async ( ) => {
136139 await seed ( {
137140 "src/index.ts" : dedent /* javascript */ `
138141 import name from "./other"
@@ -201,7 +204,7 @@ describe("BundleController", () => {
201204 ` ) ;
202205 } ) ;
203206
204- test ( "custom build" , async ( { controller } ) => {
207+ test ( "custom build" , async ( ) => {
205208 await seed ( {
206209 "random_dir/index.ts" : dedent /* javascript */ `
207210 export default {
@@ -286,7 +289,7 @@ describe("BundleController", () => {
286289 } ) ;
287290 } ) ;
288291
289- test ( "module aliasing" , async ( { controller } ) => {
292+ test ( "module aliasing" , async ( ) => {
290293 await seed ( {
291294 "src/index.ts" : dedent /* javascript */ `
292295 import name from "foo"
@@ -351,7 +354,7 @@ describe("BundleController", () => {
351354 } ) ;
352355
353356 describe ( "switching" , ( ) => {
354- test ( "esbuild -> custom builds" , async ( { controller } ) => {
357+ test ( "esbuild -> custom builds" , async ( ) => {
355358 await seed ( {
356359 "src/index.ts" : dedent /* javascript */ `
357360 export default {
@@ -486,7 +489,7 @@ describe("BundleController", () => {
486489 ` ) ;
487490 } ) ;
488491
489- test ( "custom builds -> esbuild" , async ( { controller } ) => {
492+ test ( "custom builds -> esbuild" , async ( ) => {
490493 await seed ( {
491494 "random_dir/index.ts" : dedent /* javascript */ `
492495 export default {
0 commit comments