Configuration (playwright.config.ts)
Relevant source files
The following files were used as context for generating this wiki page:
The playwright.config.ts (or .js) file is the heart of a Playwright Test project. It serves as the central location for configuring all aspects of the test execution, from defining test projects and browser settings to specifying reporters and global setup/teardown scripts. This configuration file exports a TestConfig object, which Playwright's test runner consumes to orchestrate the entire testing process.
The system is designed to be flexible, allowing for a base configuration that can be extended and overridden by specific "projects." This enables running the same test suite against multiple configurations, such as different browsers, viewports, or environments, all managed from a single file. For type safety and autocompletion, Playwright provides a defineConfig helper function, which also facilitates merging multiple configuration objects.
Configuration Loading Process
Playwright follows a specific process to locate and load the configuration file. This process is initiated by the loadConfigFromFile function, which resolves the configuration path and then proceeds to load, parse, and validate it.
The diagram below illustrates the high-level flow of loading a configuration.
This diagram shows the process from resolving the config file location to returning a fully parsed and validated configuration object.
Sources: packages/playwright/src/common/configLoader.ts:394-397
File Resolution
When a configuration file is not explicitly provided via the --config CLI flag, Playwright searches for it in the current working directory. The resolution logic, handled by resolveConfigFile, checks for the following file names in order:
playwright.config.tsplaywright.config.jsplaywright.config.mtsplaywright.config.mjsplaywright.config.ctsplaywright.config.cjs
If a directory is passed instead of a file, Playwright searches for these files within that directory. If no config file is found, the directory is treated as the test root.
Sources: packages/playwright/src/common/configLoader.ts:409-415
ESM and CJS Support
Playwright supports both CommonJS (.cjs) and ES Modules (.mjs, .mts, or files in a "type": "module" package). The loadConfig function first registers an ESM loader if required. For Node.js versions below 18.19, loading an ESM config file will result in an error, as a newer Node.js version is required for the native loader hooks.
Sources: packages/playwright/src/common/configLoader.ts:101-107
Core Configuration Structure
The configuration is defined by the TestConfig and TestProject interfaces. TestConfig represents the top-level configuration, while TestProject defines the settings for a specific project within the projects array.
TestConfig and TestProject Relationship
The following class diagram shows the relationship between the main configuration interfaces. A TestConfig can contain multiple TestProject definitions.
This diagram illustrates that a TestConfig object can hold an array of TestProject objects, each with its own specific settings that can override the top-level configuration.
Sources: packages/playwright/types/test.d.ts:1005, packages/playwright/types/test.d.ts:74
Key TestConfig Options
These options are set at the top level of playwright.config.ts and apply to all projects unless overridden.
| Option | Type | Description |
|---|---|---|
projects | Project[] | An array of test project configurations. |
testDir | string | Directory to be recursively scanned for test files. Defaults to the config file's directory. |
timeout | number | Timeout for each test in milliseconds. Defaults to 30 seconds. |
reporter | string | ReporterDescription[] | The reporter(s) to use. Can be a built-in name or a path to a custom reporter. |
use | object | Global options for all tests, such as browser settings and fixture options. |
globalSetup | string | Path to a module that is executed once before all tests. |
globalTeardown | string | Path to a module that is executed once after all tests. |
webServer | TestConfigWebServer | TestConfigWebServer[] | Launches a development web server during tests. |
workers | number | string | The maximum number of concurrent worker processes. Can be a number or a percentage. |
forbidOnly | boolean | Whether to fail the test run if test.only is found. Useful for CI. |
retries | number | The number of times to retry failed tests. |
Sources: packages/playwright/types/test.d.ts:1005-1310
Projects
Projects are the primary mechanism for running tests across multiple configurations. Each object in the projects array defines a separate test run with its own settings.
Key TestProject Options
These options can be specified within a project object. They override any corresponding options set at the top level.
| Option | Type | Description |
|---|---|---|
name | string | Project name, visible in reports. Used to filter projects from the CLI. |
use | object | Project-specific options that override the top-level use object. |
testDir | string | Directory containing tests for this project. Overrides top-level testDir. |
testMatch | string | RegExp | Array<string|RegExp> | Glob patterns or regular expressions to find test files. |
testIgnore | string | RegExp | Array<string|RegExp> | Glob patterns or regular expressions to ignore test files. |
timeout | number | Timeout for tests in this project, overriding the global timeout. |
dependencies | string[] | An array of project names that must run successfully before this project runs. |
teardown | string | The name of a project to run after this project and its dependencies have finished. |
Sources: packages/playwright/types/test.d.ts:74-565, tests/playwright-test/config.spec.ts:18-20
An example from the codebase shows how projects can be defined with specific use options and metadata.
// tests/webview2/playwright.config.ts:36-47
config.projects.push({
name: 'webview2',
// Share screenshots with chromium.
snapshotPathTemplate: '{testDir}/{testFileDir}/{testFileName}-snapshots/{arg}-chromium{ext}',
use: {
browserName: 'chromium',
headless: false,
},
testDir: path.join(testDir, 'page'),
metadata,
});
Sources: tests/webview2/playwright.config.ts:36-47
The defineConfig Helper
Playwright exports a defineConfig helper function to provide type-checking and autocompletion for the configuration object. It also supports merging multiple configuration objects, which is useful for creating a base configuration that can be extended.
// packages/playwright/src/common/configLoader.ts:31-68
export const defineConfig = (...configs: any[]) => {
let result = configs[0];
for (let i = 1; i < configs.length; ++i) {
const config = configs[i];
// ... merge logic ...
result = {
...result,
...config,
// Deep merge for 'expect', 'use', 'build', 'webServer'
};
// Special handling for merging 'projects' array by name
}
result[kDefineConfigWasUsed] = true;
return result;
};
When merging, defineConfig performs a deep merge for expect, use, and build objects. The webServer option is merged by concatenating arrays. The projects array is merged by name: if a project in a subsequent config has the same name as one in a previous config, its properties are merged into the existing project. New projects are appended to the list.
Sources: packages/playwright/src/common/configLoader.ts:31-68, tests/playwright-test/config.spec.ts:413-446
Configuration Validation
After a configuration file is loaded, Playwright validates its contents to ensure all properties have the correct types and values. The validateConfig and validateProject functions in configLoader.ts are responsible for this.
Validation checks include:
- Ensuring the config is an object.
- Verifying property types (e.g.,
forbidOnlymust be a boolean). - Checking that numeric values are non-negative (e.g.,
timeout,retries). - Validating that string properties with enumerated values are one of the allowed values (e.g.,
preserveOutputmust be'always','never', or'failures-only'). - Validating that
testMatchandtestIgnoreare strings, RegExps, or arrays thereof.
If validation fails, Playwright throws an error with a descriptive message pointing to the invalid property in the configuration file.
Sources: packages/playwright/src/common/configLoader.ts:149-258
Component Testing Configuration
For component testing, additional configuration options are available under the use property. These are typically managed by framework-specific packages like @playwright/experimental-ct-react.
Key component testing options include:
ctPort: The port for the dev server.ctTemplateDir: Directory for the test harness (index.html).ctCacheDir: Directory for the Vite build cache.ctViteConfig: A Vite configuration object or a function returning one, to customize the dev server.
These settings are resolved by resolveDirs and used by createConfig to set up the Vite dev server for mounting components.
Sources: packages/playwright-ct-core/src/viteUtils.ts:24-31, packages/playwright-ct-core/src/viteUtils.ts:51-71
Summary
The playwright.config.ts file is a powerful and essential part of using Playwright Test. It provides a structured and type-safe way to define the entire test environment. Through its support for projects, it allows for comprehensive cross-browser and cross-device testing. The configuration loader is robust, handling both CJS and ESM formats, and includes a validation step to catch common errors early. The defineConfig helper enhances the developer experience by enabling autocompletion and configuration merging, making test suite management both scalable and maintainable.