Skip to content

Commit c51ba25

Browse files
final cleanups
1 parent e692a46 commit c51ba25

File tree

12 files changed

+52
-31
lines changed

12 files changed

+52
-31
lines changed

packages/vite-plugin-cloudflare/playground/__test-utils__/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ export function failsIf(condition: boolean) {
88
return condition ? test.fails : test;
99
}
1010

11+
/**
12+
* Makes a change to a file and restores it after the test is finished.
13+
*/
1114
export function mockFileChange(
15+
/** The path to the file to change */
1216
filePath: string,
17+
/** A function that modifies the original content of the file */
1318
mutateFn: (originalContent: string) => string
1419
) {
1520
const originalContent = fs.readFileSync(filePath, "utf-8");

packages/vite-plugin-cloudflare/playground/__test-utils__/responses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { page, viteTestUrl } from "./index";
22

3+
/** Common options to use with `vi.waitFor()` */
34
export const WAIT_FOR_OPTIONS = { timeout: 5_000, interval: 500 };
45

56
export async function getTextResponse(path = "/"): Promise<string> {

packages/vite-plugin-cloudflare/playground/dev-vars/__tests__/with-specified-env/dev-vars-loading.spec.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import fs from "node:fs";
22
import { describe, expect, test, vi } from "vitest";
3-
import { getJsonResponse, isBuild, testDir } from "../../../__test-utils__";
3+
import {
4+
getJsonResponse,
5+
isBuild,
6+
testDir,
7+
WAIT_FOR_OPTIONS,
8+
} from "../../../__test-utils__";
49

510
test("reading variables from a staging .dev.vars file", async () => {
6-
await vi.waitFor(async () =>
7-
expect(await getJsonResponse()).toEqual({
8-
"variables present in .dev.vars.staging": {
9-
MY_DEV_VAR_A: "my .dev.vars staging variable A",
10-
MY_DEV_VAR_B: "my .dev.vars staging variable B",
11-
},
12-
})
11+
await vi.waitFor(
12+
async () =>
13+
expect(await getJsonResponse()).toEqual({
14+
"variables present in .dev.vars.staging": {
15+
MY_DEV_VAR_A: "my .dev.vars staging variable A",
16+
MY_DEV_VAR_B: "my .dev.vars staging variable B",
17+
},
18+
}),
19+
WAIT_FOR_OPTIONS
1320
);
1421
});
1522

packages/vite-plugin-cloudflare/playground/dot-env/__tests__/with-specified-env/vars-changes.spec.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getJsonResponse,
55
isBuild,
66
mockFileChange,
7+
WAIT_FOR_OPTIONS,
78
} from "../../../__test-utils__";
89

910
test.runIf(!isBuild)(
@@ -26,18 +27,15 @@ test.runIf(!isBuild)(
2627
content.replace(/my \.env staging/g, "my .env UPDATED staging")
2728
);
2829

29-
await vi.waitFor(
30-
async () => {
31-
const updatedResponse = await getJsonResponse();
32-
expect(updatedResponse).toEqual({
33-
"variables loaded from .env and .env.staging": {
34-
MY_DEV_VAR_A: "my .env UPDATED staging variable A",
35-
MY_DEV_VAR_B: "my .env UPDATED staging variable B",
36-
MY_DEV_VAR_C: "my .env UPDATED variable C", // Note that unlike .dev.vars, we merge .env files
37-
},
38-
});
39-
},
40-
{ timeout: 5000 }
41-
);
30+
await vi.waitFor(async () => {
31+
const updatedResponse = await getJsonResponse();
32+
expect(updatedResponse).toEqual({
33+
"variables loaded from .env and .env.staging": {
34+
MY_DEV_VAR_A: "my .env UPDATED staging variable A",
35+
MY_DEV_VAR_B: "my .env UPDATED staging variable B",
36+
MY_DEV_VAR_C: "my .env UPDATED variable C", // Note that unlike .dev.vars, we merge .env files
37+
},
38+
});
39+
}, WAIT_FOR_OPTIONS);
4240
}
4341
);

packages/vite-plugin-cloudflare/playground/node-compat/__tests__/worker-postgres/postgres.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import {
66
} from "../../../__test-utils__";
77

88
test("should be able to create a pg Client", async () => {
9-
await vi.waitFor(async () =>
10-
expect(await getTextResponse()).toMatchInlineSnapshot(
11-
`"hh-pgsql-public.ebi.ac.uk"`
12-
)
9+
await vi.waitFor(
10+
async () =>
11+
expect(await getTextResponse()).toMatchInlineSnapshot(
12+
`"hh-pgsql-public.ebi.ac.uk"`
13+
),
14+
WAIT_FOR_OPTIONS
1315
);
1416
});
1517

packages/vite-plugin-cloudflare/playground/node-compat/__tests__/worker-warnings/warnings.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dedent from "ts-dedent";
22
import { expect, test, vi } from "vitest";
3-
import { isBuild, serverLogs } from "../../../__test-utils__";
3+
import { isBuild, serverLogs, WAIT_FOR_OPTIONS } from "../../../__test-utils__";
44

55
test.skipIf(isBuild)(
66
"should display warnings if nodejs_compat is missing",
@@ -14,7 +14,7 @@ test.skipIf(isBuild)(
1414
- "perf_hooks" imported from "worker-warnings/index.ts"
1515
`
1616
),
17-
{ timeout: 5_000, interval: 500 }
17+
WAIT_FOR_OPTIONS
1818
);
1919
}
2020
);

packages/vite-plugin-cloudflare/playground/vitest.config.e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default defineConfig({
1313
globalSetup: ["./vitest-global-setup.ts"],
1414
reporters: "dot",
1515
onConsoleLog: () => debuglog.enabled,
16-
testTimeout: 100_000,
16+
testTimeout: 10000,
1717
},
1818
publicDir: false,
1919
});

packages/vite-plugin-cloudflare/src/cloudflare-environment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export class CloudflareDevEnvironment extends vite.DevEnvironment {
9191
async initRunner(
9292
worker: ReplaceWorkersTypes<Fetcher>,
9393
workerConfig: WorkerConfig,
94+
/** A unique identifier used for debugging errors when config updates. */
9495
configId: string
9596
) {
9697
this.#worker = worker;
@@ -201,6 +202,7 @@ export function initRunners(
201202
resolvedPluginConfig: WorkersResolvedConfig,
202203
viteDevServer: vite.ViteDevServer,
203204
miniflare: Miniflare,
205+
/** A unique identifier used for debugging errors when config updates. */
204206
configId: string
205207
): Promise<void[]> | undefined {
206208
return Promise.all(

packages/vite-plugin-cloudflare/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {
107107
let containerImageTagsSeen: Set<string> | undefined;
108108
let runningContainerIds: Array<string>;
109109

110+
/** Used to track whether hooks are being called because of a server restart or a server close event. */
110111
let restartingServer = false;
111112

112113
return [
@@ -343,6 +344,7 @@ if (import.meta.hot) {
343344
// Vite `configureServer` Hook
344345
// see https://vite.dev/guide/api-plugin.html#configureserver
345346
async configureServer(viteDevServer) {
347+
// Patch the `server.restart` method to track whether the server is restarting or not.
346348
const restartServer = viteDevServer.restart.bind(viteDevServer);
347349
viteDevServer.restart = async () => {
348350
try {
@@ -354,12 +356,14 @@ if (import.meta.hot) {
354356
restartingServer = false;
355357
}
356358
};
359+
357360
assertIsNotPreview(resolvedPluginConfig);
358361

359362
// It is possible to get into a situation where the dev server is restarted by a config file change
360363
// right in the middle of the Vite server and the supporting Workers being initialized.
361364
// We use an abort controller to signal to the initialization code that it should stop if the config has changed.
362365
const restartAbortController = new AbortController();
366+
363367
// We use a `configId` to help debug how the config changes are triggering the restarts.
364368
const configId = randomUUID();
365369

@@ -424,8 +428,8 @@ if (import.meta.hot) {
424428
configId,
425429
"Aborting setting up miniflare because config has changed."
426430
);
427-
// The config has changes while this was still trying to setup the server.
428-
// So just abort and allow the new server to be set up.
431+
// The config has changed while we were still trying to setup the server,
432+
// so just abort and allow the new server to be set up instead.
429433
return;
430434
}
431435

packages/vite-plugin-cloudflare/src/runner-worker/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export function createWorkerEntrypointWrapper(
132132
entrypoint: string
133133
): WorkerEntrypointConstructor<WrapperEnv> {
134134
class Wrapper extends WorkerEntrypoint<WrapperEnv> {
135+
/** A unique identifier used for debugging errors when config updates. */
135136
configId?: string;
136137
constructor(ctx: ExecutionContext, env: WrapperEnv) {
137138
super(ctx, env);

0 commit comments

Comments
 (0)