diff --git a/docs/src/test-parallel-js.md b/docs/src/test-parallel-js.md index d768c748e6a9d..c971884e57720 100644 --- a/docs/src/test-parallel-js.md +++ b/docs/src/test-parallel-js.md @@ -136,6 +136,19 @@ test.describe('runs in parallel with other describes', () => { }); ``` +## Avoiding shared state in parallel tests + +Playwright Test isolates parallel tests by default: + +- **Separate worker processes.** Workers are independent OS processes, so module-level variables, singletons and changes to `process.env` in one worker are invisible to others. +- **Isolated browser contexts.** The built-in [`page`](./test-fixtures.md#built-in-fixtures) and [`context`](./test-fixtures.md#built-in-fixtures) fixtures give every test its own [BrowserContext] with fresh cookies, local storage and permissions. + +You do not need to reset browser state or serialize tests to get this. Shared state only bites when tests reach outside these boundaries: + +- **External resources.** Parallel tests touching the same database rows, files or accounts race with each other. Give each worker its own data (see [Isolate test data between parallel workers](#isolate-test-data-between-parallel-workers)), or derive unique data per test from [`property: TestInfo.testId`]. +- **Files on disk.** Writing to a fixed path from multiple tests races. Use [`method: TestInfo.outputPath`] for a unique path scoped to the current test. +- **State shared within a worker.** In [fully parallel](#parallelize-tests-in-a-single-file) mode, tests from one file run in the same worker one after another. Worker-scoped fixtures and module-level variables are shared between them, so keep anything a test mutates in the default test scope, not in worker-scoped state. + ## Shard tests between multiple machines Playwright Test can shard a test suite, so that it can be executed on multiple machines.