ESLint plugin for Playwright.
npm
npm install -D eslint-plugin-playwright
Yarn
yarn add -D eslint-plugin-playwright
pnpm
pnpm add -D eslint-plugin-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/recommended'],
files: ['tests/**'],
rules: {
...playwright.configs['flat/recommended'].rules,
// Customize Playwright rules
// ...
},
},
]
Legacy config (.eslintrc)
{
"overrides": [
{
"files": "tests/**",
"extends": "plugin:playwright/recommended"
}
]
}
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"]
}
}
}
}
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"
}
}
}
}
β
Set in the recommended
configuration
π§ Automatically fixable by the --fix
CLI option
π‘ Manually fixable by
editor suggestions
Rule | Description | β | π§ | π‘ |
---|---|---|---|---|
expect-expect | Enforce assertion to be made in a test body | β | ||
max-expects | Enforces a maximum number assertion calls in a test body | |||
max-nested-describe | Enforces a maximum depth to nested describe calls | β | ||
missing-playwright-await | Enforce Playwright APIs to be awaited | β | π§ | |
no-commented-out-tests | Disallow commented out tests | |||
no-conditional-expect | Disallow calling expect conditionally |
β | ||
no-conditional-in-test | Disallow conditional logic in tests | β | ||
no-duplicate-hooks | Disallow duplicate setup and teardown hooks | |||
no-element-handle | Disallow usage of element handles | β | π‘ | |
no-eval | Disallow usage of page.$eval() and page.$$eval() |
β | ||
no-focused-test | Disallow usage of .only annotation |
β | π‘ | |
no-force-option | Disallow usage of the { force: true } option |
β | ||
no-get-by-title | Disallow using getByTitle() |
π§ | ||
no-hooks | Disallow setup and teardown hooks | |||
no-nested-step | Disallow nested test.step() methods |
β | ||
no-networkidle | Disallow usage of the networkidle option |
β | ||
no-nth-methods | Disallow usage of first() , last() , and nth() methods |
|||
no-page-pause | Disallow using page.pause() |
β | ||
no-raw-locators | Disallow using raw locators | |||
no-restricted-matchers | Disallow specific matchers & modifiers | |||
no-skipped-test | Disallow usage of the .skip annotation |
β | π‘ | |
no-slowed-test | Disallow usage of the .slow annotation |
β | π‘ | |
no-standalone-expect | Disallow using expect outside of test blocks |
β | ||
no-unsafe-references | Prevent unsafe variable references in page.evaluate() |
β | π§ | |
no-useless-await | Disallow unnecessary await s for Playwright methods |
β | π§ | |
no-useless-not | Disallow usage of not matchers when a specific matcher exists |
β | π§ | |
no-wait-for-selector | Disallow usage of page.waitForSelector() |
β | π‘ | |
no-wait-for-timeout | Disallow usage of page.waitForTimeout() |
β | π‘ | |
prefer-comparison-matcher | Suggest using the built-in comparison matchers | π§ | ||
prefer-equality-matcher | Suggest using the built-in equality matchers | π‘ | ||
prefer-hooks-in-order | Prefer having hooks in a consistent order | |||
prefer-hooks-on-top | Suggest having hooks before any test cases | |||
prefer-lowercase-title | Enforce lowercase test names | π§ | ||
prefer-native-locators | Suggest built-in locators over page.locator() |
π§ | ||
prefer-locator | Suggest locators over page methods | |||
prefer-strict-equal | Suggest using toStrictEqual() |
π‘ | ||
prefer-to-be | Suggest using toBe() |
π§ | ||
prefer-to-contain | Suggest using toContain() |
π§ | ||
prefer-to-have-count | Suggest using toHaveCount() |
π§ | ||
prefer-to-have-length | Suggest using toHaveLength() |
π§ | ||
prefer-web-first-assertions | Suggest using web first assertions | β | π§ | |
require-hook | Require setup and teardown code to be within a hook | |||
require-soft-assertions | Require assertions to use expect.soft() |
π§ | ||
require-to-throw-message | Require a message for toThrow() |
|||
require-top-level-describe | Require test cases and hooks to be inside a test.describe block |
|||
valid-describe-callback | Enforce valid describe() callback |
β | ||
valid-expect-in-promise | Require promises that have expectations in their chain to be valid | β | ||
valid-expect | Enforce valid expect() usage |
β | ||
valid-title | Enforce valid titles | β | π§ |