Files
KomediruzeckiandGitHub 1f52a645d8 fix(e2e): stopping the Worker webServer stops wrangler too (#588)
Every local `pnpm test:e2e` left its `wrangler dev` process group running
after Playwright exited: wrangler, its node child, esbuild and workerd,
re-parented to init and still listening on :8787. The next local run's
`reuseExistingServer` adopted that orphan, so local E2E could test
whatever code it had started with, possibly another branch's.

Playwright stops a webServer by signalling the command's process group,
and without `gracefulShutdown` the signal is SIGKILL. The Worker entry
runs serve-worker.mjs, whose supervisor stops wrangler from its
SIGINT/SIGTERM handler, and a SIGKILL runs no handler. Nothing else could
reach wrangler: the supervisor spawns it detached, in a group of its own,
so that a restart also kills the workerd it forked.

The Worker entry now sets `gracefulShutdown: { signal: 'SIGTERM',
timeout: 5000 }`. The SIGTERM reaches node through the `sh -c '... &&
...'` wrapper: bash and dash both exec the last command of the list, and
a group signal reaches every member of the group anyway. The supervisor
kills wrangler's group and exits; past the timeout Playwright still falls
back to SIGKILL. The Vite entry needs nothing: none of its processes
detach, so the group SIGKILL already takes all of them.

e2eWorkerTeardown.test.ts runs the real serve-worker.mjs and supervisor
against a stand-in wrangler that forks a stand-in workerd, stops them
the way Playwright stops the configured entry, and asserts that both are
gone and nothing answers on the port. It fails with the gracefulShutdown
line removed, and with SIGTERM dropped from the supervisor's cleanup.
2026-10-07 17:59:38 +02:00

119 lines
5.7 KiB
TypeScript

import { defineConfig, devices } from '@playwright/test'
import os from 'os'
import {
E2E_API_BASE,
E2E_API_PORT,
E2E_BASE,
E2E_PORT,
STORAGE_STATE,
} from './tests/e2e-constants'
/**
* See https://playwright.dev/docs/test-configuration.
*
* The suite drives the REAL built app against the REAL API — the Cloudflare Worker, run locally by
* wrangler against a local D1. It used to point at the Express server under `backend/`, which was
* the last thing keeping that retired runtime in the tree, and which meant 35 spec files were
* exercising an implementation nothing ships.
*
* Two servers, in order: wrangler on :8787, then vite on :3800 with its /api proxy pinned at it.
* Then a setup project signs in once and saves the state every spec starts from.
*/
export default defineConfig({
timeout: 60000,
testDir: './tests',
fullyParallel: true,
forbidOnly: process.env.CI !== undefined,
// Two retries on CI, none locally. One retry covers a single dropped fetch. The second exists
// for the wrangler crash: the Worker is supervised and comes back within a few seconds (see the
// webServer command below), but a spec caught in that gap fails, and its immediate retry can
// land in the same gap before the restart finishes. A third attempt clears that residual.
// Locally, retries only hide the flake you are trying to see.
retries: process.env.CI ? 2 : 0,
// Capped, not one-per-CPU. Every worker drives the same local D1 through one wrangler process,
// and a machine with a lot of cores turned that into enough contention that pages timed out
// waiting for their first fetch — which reads as "the goals list is empty", not as "slow".
//
// CI gets ONE. A GitHub runner is 4 vCPU / 16 GB and has to hold vite, wrangler's workerd and
// one headless Chromium per worker at the same time; at four, workerd was killed outright
// twenty seconds in (`wrangler dev` printed an empty error and exited), after which every
// remaining spec failed on a 502 from the vite proxy. Nothing restarts it mid-run, so one
// crash costs the whole suite — which makes the serial run the cheaper trade.
workers: process.env.CI ? 1 : Math.min(4, os.cpus().length),
// A backstop, no longer the main defence. The webServer command below now supervises wrangler
// and restarts it within seconds of the crash that used to end a shard, so "one death fails
// every remaining spec" is gone — the case this guarded against on 2026-08-25, when a workerd
// death a minute into shard 3 left it failing 120 specs against a dead proxy for 22 minutes.
// What remains is the ordinary one: ten genuine failures already means something is broken, and
// stopping there beats grinding through the rest. Kept for that, not for the crash.
maxFailures: process.env.CI ? 10 : undefined,
reporter: 'list',
use: {
baseURL: E2E_BASE,
trace: 'retain-on-failure',
headless: true,
testIdAttribute: 'data-test-id',
},
projects: [
{ name: 'setup', testMatch: /global\.setup\.ts/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'], storageState: STORAGE_STATE },
dependencies: ['setup'],
// The release scope lives in its own project: it runs every case in both storage modes and
// seeds a local-first demo per case, which is minutes of work CI should not pay on every push.
testIgnore: /release\//,
},
// `pnpm run test:e2e:release`: the manual dev test scope for a release, automated
// (tests/release/README.md). Only present when asked for.
...(process.env.E2E_RELEASE
? [
{
name: 'release',
testDir: './tests/release',
// The wrangler crash (see webServer below) fails whatever case is mid-request; a retry
// absorbs it and the report still lists the case as flaky, so nothing is hidden.
retries: 2,
use: { ...devices['Desktop Chrome'], storageState: STORAGE_STATE },
dependencies: ['setup'],
},
]
: []),
],
webServer: [
{
// Local D1 and R2 under worker/.wrangler — no network, no Cloudflare account. Migrations
// are applied first because a fresh checkout has an empty database and every route 500s
// against one. JWT_SECRET comes from worker/.dev.vars.
//
// Not `wrangler dev` directly: it crashes a minute or two into a run and nothing here would
// restart it, which turned one death into a whole failed shard. serve-worker.mjs supervises
// it — same babysitter the tour gate uses — and brings it back within seconds. It runs from
// frontend/ (its imports live there); the migrate step stays in the worker via `-C`.
command: `pnpm -C ../worker run d1:migrate:local && node scripts/serve-worker.mjs`,
url: `${E2E_API_BASE}/api/health`,
reuseExistingServer: !process.env.CI,
timeout: 120000,
stdout: 'pipe',
stderr: 'pipe',
// SIGTERM at the end of the run, where Playwright would otherwise SIGKILL this command's
// process group. The supervisor stops wrangler from a SIGTERM handler, and a SIGKILL runs no
// handler; wrangler lives in a group of its own, so nothing else reaches it. Without this,
// every local run left a wrangler holding :8787, and the next run's reuseExistingServer
// adopted it, stale code and all. Guarded by src/__tests__/e2eWorkerTeardown.test.ts.
gracefulShutdown: { signal: 'SIGTERM', timeout: 5000 },
},
{
command: `npm run dev -- --port ${E2E_PORT} --strictPort`,
url: E2E_BASE,
reuseExistingServer: !process.env.CI,
timeout: 120000,
stdout: 'pipe',
stderr: 'pipe',
env: { API_PROXY_TARGET: E2E_API_BASE },
},
],
})