Skip to content

Commit 74b0754

Browse files
Pbd/test configcontroller flakes (#10739)
* attempt to reduce configcontroller test flakes * focus and force test * deflake bundle controller tests * bump * logging and error handling * turn all tests back on * remove hack * remove logs
1 parent a57149f commit 74b0754

File tree

4 files changed

+60
-22
lines changed

4 files changed

+60
-22
lines changed

packages/wrangler/src/__tests__/api/startDevWorker/BundleController.test.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { once } from "events";
22
import path from "path";
33
import dedent from "ts-dedent";
4-
import { test as base, describe } from "vitest";
4+
import { describe, test } from "vitest";
55
import { BundlerController } from "../../../api/startDevWorker/BundlerController";
66
import { mockConsoleMethods } from "../../helpers/mock-console";
77
import { 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-
3019
async 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 {

packages/wrangler/src/__tests__/api/startDevWorker/ConfigController.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ describe("ConfigController", () => {
2323
mockAccountId();
2424
mockApiToken();
2525

26+
// We are not using `test.extend` or `onTestFinished` helpers here to create and tear down
27+
// the controller because these run the teardown after all the `afterEach()` blocks have run.
28+
// This means that the controller doesn't get torn down until after the temporary directory has been
29+
// removed.
30+
// And so the file watchers that the controller creates can randomly fail because they are trying to
31+
// watch files in a directory that no longer exists.
32+
// By doing it ourselves in `beforeEach()` and `afterEach()` we can ensure the controller
33+
// is torn down before the temporary directory is removed.
2634
let controller: ConfigController;
2735
beforeEach(() => {
2836
controller = new ConfigController();

packages/wrangler/src/api/startDevWorker/BundlerController.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,36 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
342342
});
343343
}
344344

345-
void this.#startCustomBuild(event.config);
346-
void this.#startBundle(event.config);
347-
void this.#ensureWatchingAssets(event.config);
345+
void this.#startCustomBuild(event.config).catch((err) => {
346+
logger.error("Failed to run custom build:", err);
347+
this.emitErrorEvent({
348+
type: "error",
349+
reason: "Failed to run custom build",
350+
cause: castErrorCause(err),
351+
source: "BundlerController",
352+
data: { config: event.config },
353+
});
354+
});
355+
void this.#startBundle(event.config).catch((err) => {
356+
logger.error("Failed to start bundler:", err);
357+
this.emitErrorEvent({
358+
type: "error",
359+
reason: "Failed to start bundler",
360+
cause: castErrorCause(err),
361+
source: "BundlerController",
362+
data: { config: event.config },
363+
});
364+
});
365+
void this.#ensureWatchingAssets(event.config).catch((err) => {
366+
logger.error("Failed to watch assets:", err);
367+
this.emitErrorEvent({
368+
type: "error",
369+
reason: "Failed to watch assets",
370+
cause: castErrorCause(err),
371+
source: "BundlerController",
372+
data: { config: event.config },
373+
});
374+
});
348375
}
349376

350377
async teardown() {

packages/wrangler/src/api/startDevWorker/ConfigController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ async function resolveConfig(
309309
script: input.entrypoint,
310310
moduleRoot: input.build?.moduleRoot,
311311
// getEntry only needs to know if assets was specified.
312-
// The actualy value is not relevant here, which is why not passing
312+
// The actual value is not relevant here, which is why not passing
313313
// the entire Assets object is fine.
314314
assets: input?.assets,
315315
},

0 commit comments

Comments
 (0)