|
1 | | -// Turbo's env var linting isn't very sophisticated. |
2 | | -// This file adds environment variables that are declared in Wrangler's turbo.json |
3 | | -/* eslint-disable turbo/no-undeclared-env-vars */ |
4 | | - |
5 | | -/** |
6 | | - * Turbo only supports caching on the individual task level, but for Wrangler's |
7 | | - * e2e tests we want to support caching on a more granular basis—at the file level. |
8 | | - * |
9 | | - * As such, we run the `test:e2e` turbo task multiple times—once per e2e test file |
10 | | - * with different arguments, ensuring that each file's tests can be cached individually. |
11 | | - * |
12 | | - * The intended flow here is that CI will run this file, which will trigger turbo to run |
13 | | - * an individual task for each Wrangler e2e test file, using `execSync`. |
14 | | - */ |
15 | 1 | import { execSync } from "child_process"; |
16 | 2 | import { readdirSync } from "fs"; |
17 | | - |
18 | | -// Get a list of e2e test files, each of which should have an associated script |
19 | | -const e2eTests = readdirSync("packages/wrangler/e2e"); |
20 | | - |
21 | | -const tasks = new Set<string>(); |
22 | | - |
23 | | -for (const file of e2eTests) { |
24 | | - // Ignore other files in the e2e directory (the README, for instance) |
25 | | - if (file.endsWith(".test.ts")) { |
26 | | - tasks.add(`e2e/${file}`); |
27 | | - } |
28 | | -} |
| 3 | +import type { ExecSyncOptionsWithBufferEncoding } from "child_process"; |
| 4 | + |
| 5 | +// Turbo only supports caching on the individual task level, but for Wrangler's |
| 6 | +// e2e tests we want to support caching on a more granular basis - at the file level. |
| 7 | +// |
| 8 | +// As such, we run the `test:e2e` turbo task multiple times — once per e2e test file so that each file's tests can be cached individually. |
| 9 | +// We use the `WRANGLER_E2E_TEST_FILE` environment variable to pass the specific test file to the e2e test runner so that it reuses the cached build tasks. |
| 10 | +// If you use a command line argument to do this turbo will create a different cache key for the build tasks. |
| 11 | +// |
| 12 | +// The intended flow here is that CI will run this file, which will trigger turbo to run |
| 13 | +// an individual task for each Wrangler e2e test file, using `execSync`. |
| 14 | +// |
| 15 | +// Any params after a `--` will be passed to the Vitest runner, so you can use this to configure the test run. |
| 16 | +// For example to update the snapshots for all Wrangler e2e tests, you can run: |
| 17 | +// |
| 18 | +// ```bash |
| 19 | +// pnpm test:e2e:wrangler -- -u |
| 20 | +// ``` |
| 21 | + |
| 22 | +const extraParamsIndex = process.argv.indexOf("--"); |
| 23 | +const extraParams = |
| 24 | + extraParamsIndex === -1 ? [] : process.argv.slice(extraParamsIndex); |
| 25 | +const command = |
| 26 | + `pnpm test:e2e --log-order=stream --output-logs=new-only --summarize --filter wrangler ` + |
| 27 | + extraParams.join(" "); |
29 | 28 |
|
30 | 29 | const failed: string[] = []; |
| 30 | +for (const file of readdirSync("packages/wrangler/e2e")) { |
| 31 | + if (!file.endsWith(".test.ts")) { |
| 32 | + // Ignore other files in the e2e directory (the README, for instance) |
| 33 | + continue; |
| 34 | + } |
31 | 35 |
|
32 | | -// If the user passes arguments to this script, use those to configure Vitest rather than running each test file individually |
33 | | -const hasCustomVitestArguments = process.argv[2]; |
34 | | - |
35 | | -const commandSuffix = hasCustomVitestArguments |
36 | | - ? ` -- --no-file-parallelism ${process.argv.slice(2).join(" ")}` |
37 | | - : ""; |
38 | | -const command = `pnpm test:e2e --log-order=stream --output-logs=new-only --summarize --filter wrangler${commandSuffix}`; |
39 | | - |
40 | | -// Add the default environment configuration for E2E tests. |
41 | | -// Most of these rely on Turbo being set up correctly to build Wrangler & C3: |
42 | | -// https://github.com/cloudflare/workers-sdk/tree/main/packages/wrangler/turbo.json#L61-L62 |
43 | | -process.env.WRANGLER ??= `node --no-warnings ${process.cwd()}/packages/wrangler/bin/wrangler.js`; |
44 | | -process.env.WRANGLER_IMPORT ??= `${process.cwd()}/packages/wrangler/wrangler-dist/cli.js`; |
45 | | -process.env.MINIFLARE_IMPORT ??= `${process.cwd()}/packages/miniflare/dist/src/index.js`; |
46 | | - |
47 | | -if (hasCustomVitestArguments) { |
48 | | - execSync(command, { |
| 36 | + const WRANGLER_E2E_TEST_FILE = `e2e/${file}`; |
| 37 | + const options: ExecSyncOptionsWithBufferEncoding = { |
49 | 38 | stdio: "inherit", |
50 | | - env: { ...process.env }, |
51 | | - }); |
52 | | - process.exit(0); |
53 | | -} |
54 | | -for (const file of tasks) { |
55 | | - console.log("::group::Testing: " + file); |
| 39 | + env: { ...process.env, WRANGLER_E2E_TEST_FILE }, |
| 40 | + }; |
| 41 | + |
| 42 | + console.log("::group::Testing: " + WRANGLER_E2E_TEST_FILE); |
56 | 43 | try { |
57 | | - execSync(command, { |
58 | | - stdio: "inherit", |
59 | | - env: { ...process.env, WRANGLER_E2E_TEST_FILE: file }, |
60 | | - }); |
| 44 | + execSync(command, options); |
61 | 45 | } catch { |
62 | 46 | console.error("Task failed - retrying"); |
63 | 47 | try { |
64 | | - execSync(command, { |
65 | | - stdio: "inherit", |
66 | | - env: { ...process.env, WRANGLER_E2E_TEST_FILE: file }, |
67 | | - }); |
| 48 | + execSync(command, options); |
68 | 49 | } catch { |
69 | 50 | console.error("Still failed, moving on"); |
70 | 51 | failed.push(file); |
|
0 commit comments