playwright.config.js


Overview

This file serves as the configuration file for Playwright Test, a testing framework for end-to-end (E2E) testing of web applications. It defines how Playwright runs tests, manages the test environment, and reports results. The configuration is tailored for a Next.js application started on a local development server, with specific settings to optimize test execution both locally and on continuous integration (CI) environments.

Key functionalities include:


Detailed Explanation

Imported Modules

import { defineConfig, devices } from '@playwright/test'

Default Export: Playwright Test Configuration Object

The file exports a single configuration object created with defineConfig. This object drives Playwright's behavior when running tests.

export default defineConfig({ ... })

Configuration Properties

1. webServer

webServer: {
  command: 'pnpm next start e2e/site --port 4000',
  reuseExistingServer: !process.env.CI,
  port: 4000
}

2. testDir

testDir: './e2e'

3. snapshotDir

snapshotDir: './e2e/__snapshots__'

4. timeout

timeout: 10 * 1000

5. fullyParallel

fullyParallel: true

6. forbidOnly

forbidOnly: !!process.env.CI

7. retries

retries: process.env.CI ? 2 : 0

8. workers

workers: process.env.CI ? 1 : undefined

9. reporter

reporter: process.env.CI
  ? [['github'], ['html', { open: 'on-failure' }]]
  : [['html', { open: 'on-failure' }]]

10. use

use: {
  baseURL: 'http://localhost:4000',
  trace: process.env.CI ? 'on-first-retry' : 'on',
  ...devices['Desktop Chrome']
}

Usage Examples

Running Tests Locally

  1. Start tests with:

npx playwright test
  1. Playwright starts the Next.js server on port 4000 (if not already running).

  2. Tests run in parallel, using Desktop Chrome emulation.

  3. HTML reports open automatically on test failures.

  4. Tracing is enabled for all tests.


Running Tests in CI Environment

  1. CI sets the CI environment variable (e.g., CI=true).

  2. Playwright will:

    • Reuse the server only if it's running.

    • Run tests serially (workers: 1).

    • Retry failed tests twice.

    • Fail the build if .only tests are present.

    • Use GitHub Actions reporter for test annotations.

    • Collect trace only on the first retry.

  3. Generates HTML reports accessible for diagnostics.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[playwright.config.js] --> B[webServer]
    A --> C[testDir]
    A --> D[snapshotDir]
    A --> E[timeout]
    A --> F[fullyParallel]
    A --> G[forbidOnly]
    A --> H[retries]
    A --> I[workers]
    A --> J[reporter]
    A --> K[use]

    B --> B1[command: pnpm next start e2e/site --port 4000]
    B --> B2[reuseExistingServer: !CI]
    B --> B3[port: 4000]

    J --> J1[CI: github + html reporter]
    J --> J2[Local: html reporter]

    K --> K1[baseURL: http://localhost:4000]
    K --> K2[trace: on-first-retry (CI), on (local)]
    K --> K3[devices['Desktop Chrome']]

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333,stroke-width:1px
    style J fill:#bbf,stroke:#333,stroke-width:1px
    style K fill:#bbf,stroke:#333,stroke-width:1px

Summary

playwright.config.js is the central configuration for running Playwright E2E tests on a Next.js app. It defines how the app server is started, controls test directories, timeouts, retries, parallelism, and reporting, adapting behavior based on the environment (local or CI). It leverages Playwright's powerful device emulation and tracing features to ensure reliable and debuggable test executions. This file is crucial for maintaining a smooth and consistent test workflow within the project.