|
| 1 | +# Playwright E2E Sharding |
| 2 | + |
| 3 | +Use `scripts/nx/playwright-plugin.ts` to turn a Playwright e2e project into Nx shard targets. |
| 4 | + |
| 5 | +The plugin looks for `nx.metadata.playwrightShards` in a project's `package.json`. When present, it generates: |
| 6 | + |
| 7 | +- `test:e2e--shard-1-of-N` through `test:e2e--shard-N-of-N` |
| 8 | +- a parent `test:e2e` target that depends on all shard targets |
| 9 | + |
| 10 | +Each shard target runs Playwright with: |
| 11 | + |
| 12 | +- `--shard=<index>/<count>` |
| 13 | +- a shard-specific `E2E_PORT_KEY` |
| 14 | +- a dependency on `build` and `^build` |
| 15 | + |
| 16 | +## 1. Enable sharding in `package.json` |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "name": "tanstack-react-start-e2e-rsc", |
| 21 | + "nx": { |
| 22 | + "metadata": { |
| 23 | + "playwrightShards": 6 |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +The package name is used to build the shard port key: |
| 30 | + |
| 31 | +```txt |
| 32 | +<package-name>-shard-<index>-of-<count> |
| 33 | +``` |
| 34 | + |
| 35 | +## 2. Do not build inside `playwright.config.ts` |
| 36 | + |
| 37 | +The plugin already makes each shard depend on `build`, so the Playwright `webServer` command should only start the built app. |
| 38 | + |
| 39 | +Good: |
| 40 | + |
| 41 | +```ts |
| 42 | +webServer: { |
| 43 | + command: `PORT=${PORT} pnpm start`, |
| 44 | + url: baseURL, |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Avoid: |
| 49 | + |
| 50 | +```ts |
| 51 | +webServer: { |
| 52 | + command: `PORT=${PORT} pnpm build && PORT=${PORT} pnpm start`, |
| 53 | + url: baseURL, |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Running the build inside every shard causes unnecessary work and can create races when multiple shards share the same project directory. |
| 58 | + |
| 59 | +## 3. Use `E2E_PORT_KEY` for every derived port |
| 60 | + |
| 61 | +If the project uses `getTestServerPort`, `getDummyServerPort`, `e2eStartDummyServer`, or `e2eStopDummyServer`, key them off `process.env.E2E_PORT_KEY` first. |
| 62 | + |
| 63 | +Example: |
| 64 | + |
| 65 | +```ts |
| 66 | +import { getTestServerPort } from '@tanstack/router-e2e-utils' |
| 67 | +import packageJson from './package.json' with { type: 'json' } |
| 68 | + |
| 69 | +const e2ePortKey = process.env.E2E_PORT_KEY ?? packageJson.name |
| 70 | +const PORT = await getTestServerPort(e2ePortKey) |
| 71 | +``` |
| 72 | + |
| 73 | +Dummy server setup/teardown should use the same key: |
| 74 | + |
| 75 | +```ts |
| 76 | +await e2eStartDummyServer(process.env.E2E_PORT_KEY ?? packageJson.name) |
| 77 | +await e2eStopDummyServer(process.env.E2E_PORT_KEY ?? packageJson.name) |
| 78 | +``` |
| 79 | + |
| 80 | +Without this, multiple shards on the same runner will reuse the same `port-*.txt` files and tear each other down. |
| 81 | + |
| 82 | +## 4. Clean stale port files once per Playwright run |
| 83 | + |
| 84 | +If the project allocates ports through `@tanstack/router-e2e-utils`, clean stale `port-*.txt` files before resolving the port. |
| 85 | + |
| 86 | +Important: do this only in the main Playwright config load. Playwright loads the config more than once, and unconditional cleanup can remap the port after the web server has already started. |
| 87 | + |
| 88 | +Example: |
| 89 | + |
| 90 | +```ts |
| 91 | +import fs from 'node:fs' |
| 92 | +import packageJson from './package.json' with { type: 'json' } |
| 93 | + |
| 94 | +const e2ePortKey = process.env.E2E_PORT_KEY ?? packageJson.name |
| 95 | + |
| 96 | +if (process.env.TEST_WORKER_INDEX === undefined) { |
| 97 | + for (const portFile of [ |
| 98 | + `port-${e2ePortKey}.txt`, |
| 99 | + `port-${e2ePortKey}-external.txt`, |
| 100 | + ]) { |
| 101 | + fs.rmSync(portFile, { force: true }) |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Avoid broad cleanup such as `rm -rf port*.txt` in shared shard runs. |
| 107 | + |
| 108 | +## 5. Keep the base URL derived from the shard key |
| 109 | + |
| 110 | +```ts |
| 111 | +const PORT = await getTestServerPort(e2ePortKey) |
| 112 | +const baseURL = `http://localhost:${PORT}` |
| 113 | + |
| 114 | +export default defineConfig({ |
| 115 | + use: { |
| 116 | + baseURL, |
| 117 | + }, |
| 118 | + webServer: { |
| 119 | + command: `PORT=${PORT} pnpm start`, |
| 120 | + url: baseURL, |
| 121 | + }, |
| 122 | +}) |
| 123 | +``` |
| 124 | + |
| 125 | +## 6. Run the generated targets |
| 126 | + |
| 127 | +Examples: |
| 128 | + |
| 129 | +```sh |
| 130 | +pnpm nx run tanstack-react-start-e2e-rsc:test:e2e--shard-1-of-6 |
| 131 | +pnpm nx run tanstack-react-start-e2e-rsc:test:e2e |
| 132 | +``` |
| 133 | + |
| 134 | +## Checklist |
| 135 | + |
| 136 | +- add `nx.metadata.playwrightShards` to `package.json` |
| 137 | +- make sure the project has a working `build` target |
| 138 | +- remove `build` from `webServer.command` |
| 139 | +- read ports from `process.env.E2E_PORT_KEY ?? packageJson.name` |
| 140 | +- use the same key for helper servers in setup and teardown |
| 141 | +- clean shard-specific `port-*.txt` files once, guarded by `TEST_WORKER_INDEX === undefined` |
| 142 | +- avoid wildcard port cleanup in shared shard runs |
| 143 | + |
| 144 | +## Reference |
| 145 | + |
| 146 | +Working example: |
| 147 | + |
| 148 | +- `e2e/react-start/rsc/package.json` |
| 149 | +- `e2e/react-start/rsc/playwright.config.ts` |
| 150 | +- `e2e/react-start/rsc/tests/setup/global.setup.ts` |
| 151 | +- `e2e/react-start/rsc/tests/setup/global.teardown.ts` |
0 commit comments