What is eslint-plugin-playwright?
eslint-plugin-playwright is an ESLint plugin that provides linting rules specific to Playwright, a Node.js library for browser automation. This plugin helps ensure best practices and code quality when writing tests with Playwright.
What are eslint-plugin-playwright's main functionalities?
No Page Pause
This rule disallows the use of `page.pause()` in your Playwright tests to prevent unintended pauses during test execution.
module.exports = {
plugins: ['playwright'],
rules: {
'playwright/no-page-pause': 'error'
}
};
No Element Handle
This rule discourages the use of `elementHandle` in favor of using locators, which are more robust and easier to work with.
module.exports = {
plugins: ['playwright'],
rules: {
'playwright/no-element-handle': 'error'
}
};
No Wait For Timeout
This rule prevents the use of `page.waitForTimeout()` to avoid flaky tests caused by arbitrary wait times.
module.exports = {
plugins: ['playwright'],
rules: {
'playwright/no-wait-for-timeout': 'error'
}
};
Other packages similar to eslint-plugin-playwright
eslint-plugin-jest
eslint-plugin-jest provides linting rules specific to Jest, a popular JavaScript testing framework. It helps enforce best practices and code quality in Jest tests. While eslint-plugin-playwright focuses on Playwright-specific rules, eslint-plugin-jest is tailored for Jest.
eslint-plugin-cypress
eslint-plugin-cypress offers linting rules for Cypress, another end-to-end testing framework. It ensures best practices and code quality in Cypress tests. Similar to eslint-plugin-playwright, but for Cypress instead of Playwright.
eslint-plugin-testing-library
eslint-plugin-testing-library provides linting rules for Testing Library, a set of utilities for testing UI components. It helps ensure best practices and code quality when using Testing Library. While eslint-plugin-playwright is for Playwright, this plugin is for Testing Library.
ESLint Plugin Playwright
ESLint plugin for Playwright.
Installation
npm
npm install -D eslint-plugin-playwright
Yarn
yarn add -D eslint-plugin-playwright
pnpm
pnpm add -D eslint-plugin-playwright
Usage
This plugin bundles two configurations to work with both @playwright/test
or
jest-playwright
. The recommended setup is to use the files
field to target
only Playwright test files. In the examples below, this is done by targeting
files in the tests
directory and only applying the Playwright rules to those
files. In your project, you may need to change the files
field to match your
Playwright test file patterns.
Flat config
(eslint.config.js)
import playwright from 'eslint-plugin-playwright'
export default [
{
...playwright.configs['flat/playwright'],
files: ['tests/**'],
},
{
files: ['tests/**'],
rules: {
},
},
]
Legacy config
(.eslintrc)
{
"overrides": [
{
"files": "tests/**",
"extends": "plugin:playwright/recommended"
}
]
}
Flat config
(eslint.config.js)
import playwright from 'eslint-plugin-playwright'
import jest from 'eslint-plugin-jest'
export default [
{
...playwright.configs['flat/jest-playwright'],
files: ['tests/**'],
},
{
files: ['tests/**'],
plugins: { jest },
rules: {
},
},
]
Legacy config
(.eslintrc)
{
"overrides": [
{
"files": "tests/**",
"extends": "plugin:playwright/jest-playwright"
}
]
}
Settings
Aliased Playwright Globals
If you import Playwright globals (e.g. test
, expect
) with a custom name, you
can configure this plugin to be aware of these additional names.
{
"settings": {
"playwright": {
"globalAliases": {
"test": ["myTest"],
"expect": ["myExpect"]
}
}
}
}
Custom Messages
You can customize the error messages for rules using the
settings.playwright.messages
property. This is useful if you would like to
increase the verbosity of error messages or provide additional context.
Only the message ids you define in this setting will be overridden, so any other
messages will use the default message defined by the plugin.
{
"settings": {
"playwright": {
"messages": {
"conditionalExpect": "Avoid conditional expects as they can lead to false positives"
}
}
}
}
Rules
✅ Set in the recommended
configuration
🔧 Automatically fixable by the --fix
CLI option
💡 Manually fixable by
editor suggestions