What happens under the hood of the runner
The runner is a main process that collects the tests and a pool of workers: separate Node.js processes, each with its own browser. Different files run in parallel, while tests of one file run in order in one worker. After a failure the worker exits and a new one starts, so beforeAll runs again and module variables are not shared between workers.
Playwright · Architecture
A test failed and beforeAll created the test user a second time. Is that a bug?
CI logs after one red test show the account created twice, and a variable written by one test is empty in the neighbouring one. This is not a glitch but a consequence of how the Playwright Test runner distributes work: a main process and a pool of workers. The lesson explains that layout, what happens after a failure, and how to write data setup that survives it.
How it works and why. npx playwright test starts the main process. It reads the config, finds the test files, builds the list of tests and hands them out to workers. A worker is a separate Node.js process: it imports the test files itself, launches its own browser, runs the tests it got and sends results back, and the main process passes them to reporters. The default number of workers is half of the logical cores; change it with the workers option or the --workers flag.
Who does what
By default, different files run in parallel in different workers, while tests inside one file run sequentially in one worker. To parallelise tests of a single file too, enable fullyParallel: true or test.describe.configure({ mode: 'parallel' }) for a group: then neighbouring tests of one file can land in different processes.
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
fullyParallel: true, // parallelise tests inside a file too
workers: process.env.CI ? 4 : undefined, // undefined = half of the cores
retries: process.env.CI ? 2 : 0,
});
What happens after a failure. When a test fails or hits its timeout, the worker is shut down and a new process starts for the following tests. This keeps "broken" state (a hung process, a half-cleaned context, corrupted global state) from leaking into other tests. A side effect: beforeAll/afterAll and worker fixtures run again, now in the new worker. Retries follow the same principle: a failed test is repeated in a new worker with a clean browser.
Worker lifecycle around one failure
Example: expensive setup in beforeAll. A test user is created through the API, and its id lives in a module variable.
// before
let userId: string; // every worker has its own copy
test.beforeAll(async ({ request }) => {
const res = await request.post('/api/test-users');
userId = (await res.json()).id;
});
There are two problems: after every failure the user is created again, and the link "several tests need the user" rests on a variable that exists only inside this worker. It is sturdier to move setup into a worker fixture: it is created once per worker, has explicit cleanup and unique data:
// after: fixtures.ts
import { test as base } from '@playwright/test';
type Account = { id: string; email: string };
export const test = base.extend<{}, { account: Account }>({
account: [
async ({ playwright }, use, workerInfo) => {
const api = await playwright.request.newContext({
baseURL: process.env.API_URL,
});
const res = await api.post('/api/test-users', {
data: { email: 'qa+w' + workerInfo.workerIndex + '@example.com' },
});
const account: Account = await res.json();
await use(account);
await api.delete('/api/test-users/' + account.id);
await api.dispose();
},
{ scope: 'worker' },
],
});
Why this is better: the data is unique per worker (workerIndex differs for every process, even a restarted one), cleanup sits next to creation, and a test simply asks for account in its arguments. parallelIndex, by contrast, is a slot from 0 to workers - 1 that is reused after a restart, so it suits picking an account from a pre-prepared pool.
When tests must stay together. Sometimes steps really are dependent, such as a multi-step form. For that there is test.describe.configure({ mode: 'serial' }): tests of the group run in order in one worker, after the first failure the rest are skipped, and a retry restarts the group from the first test. This is an exception: serial groups parallelise badly, and one red step hides the results of the next ones. Alternatives: one test with test.step, or independent tests with data from fixtures or the API.
Limits. Workers scale execution on one machine. For several machines there is sharding (--shard=1/4), where each machine runs its own part of the tests with its own pool of workers. Workers do not isolate the server: if they all use one account, they fight over the same data. A retry does not cure instability caused by server data, it only masks it in the summary, so flaky tests in the report should be investigated, not counted as green. This is where Playwright differs from setups in which one process runs all tests in a row: there, state drifts between tests unnoticed, while here processes cut it off.
Common mistakes.
- Writing a module variable in one test and reading it in another: the second may land in another worker and see nothing.
- Doing expensive setup in
beforeAllwithout accounting for it re-running after every failure. A worker fixture with cleanup is better. - Relying on file order: the distribution across workers is not guaranteed.
- One shared test account for all workers: parallel tests change the same data. Split data with
workerIndexorparallelIndex. - Putting
mode: 'serial'everywhere "for reliability": instead of isolation you get a chain where the first failure removes the rest.
Check yourself
A file has three tests and beforeAll creates a user. The second test fails, no retries. How many times does beforeAll run, and why?
Answer
Twice: after the failure the worker is shut down, and the third test runs in a new worker, which runs beforeAll again. For one-time setup with cleanup, use a worker fixture.
Test A writes to a module variable token, test B reads it and gets undefined. Under what conditions does this happen even if A runs first?
Answer
If B lands in another worker (fullyParallel, another file or a restart after a failure): every process has its own copy of the variable. Data cannot be passed between tests through module variables: use a fixture or the API.
Why does a retry not mask leftovers of the previous run in the browser, yet it can mask a data problem on the server?
Answer
The retry runs in a new worker with a clean browser, so cookies and storage do not carry over. Server data survives the restart: instability caused by it slips through the retry, and the test is marked flaky instead of green.
beforeAll can run several times, and module variables are not shared between workers. I keep no shared state in variables: I prepare data with worker fixtures that clean up and use unique data through workerIndex, and I use a serial group only for truly dependent steps. To scale across several machines I use sharding.