Fixture Scopes, Parallelism and Isolation

๐Ÿ“‹ Table of Contents โ–พ
  1. Fixture Scopes and Parallel Workers
  2. Common Mistakes

Fixture scopes and parallelism control how often setup runs and which tests share resources. For large suites, getting this right is critical for both speed and isolation.

Fixture Scopes and Parallel Workers

Playwright supports different fixture scopes, most commonly per-test and per-worker. Per-test fixtures run freshly for each test, while per-worker fixtures are created once per worker process and reused across tests on that worker.

// scoped-fixtures.ts
import { test as base } from '@playwright/test';

// Example of a worker-scoped fixture for expensive setup.
const test = base.extend({
  globalConfig: [async ({}, use) => {
    const config = { apiBaseUrl: 'https://api.demo.myshop.com' };
    await use(config);
  }, { scope: 'worker' }],
});

export { test };
Note: Worker-scoped fixtures are ideal for expensive initialisation that is safe to share, such as configuration or read-only test data.
Tip: Start with per-test fixtures by default, then move specific cases to worker scope if you identify safe sharing opportunities.
Warning: Sharing mutable resources (like a single user account) across parallel tests through worker fixtures can cause race conditions and flakiness.

Understanding how Playwright distributes tests across workers helps you design fixtures that balance performance with isolation.

Common Mistakes

Mistake 1 โ€” Using worker scope for mutable, shared state

This creates hidden couplings.

โŒ Wrong: Reusing a single shopping cart or user session across many tests in parallel.

โœ… Correct: Keep such state per test or use unique data per worker.

Mistake 2 โ€” Making all fixtures per-test without considering cost

This can slow down suites.

โŒ Wrong: Re-running extremely heavy setup thousands of times.

โœ… Correct: Identify safe candidates for worker scope or one-time setup.

🧠 Test Yourself

How should you approach fixture scopes in a parallel Playwright suite?