Playwright 规则
eslint-plugin-playwright 插件是为使用Playwright进行 E2E 测试的项目而设计的。它提供了 Playwright 官方推荐的规则,确保 Playwright 测试代码符合最佳实践,从而提高测试的可靠性和可读性。
// .eslintrc.js
module.exports = {
extends: [
'plugin:playwright/recommended',
],
};
注意
在编写 Playwright 代码时,可以适当关闭一些规则,避免过于严格的检查影响开发效率。但是,仍然鼓励遵循这些规范,以提高代码质量和可维护性。
| 规则名称 | 错误级别 | 配置选项 | 描述 |
|---|---|---|---|
| no-empty-pattern | off | - | 禁止对象/数组解构中使用空模式(ESLint 核心规则) |
| playwright/expect-expect | warn | - | 强制在测试中至少有一个 expect 断言 |
| playwright/max-nested-describe | warn | - | 限制 describe 块的嵌套层级深度 |
| playwright/missing-playwright-await | error | - | 强制对 Playwright 异步操作使用 await |
| playwright/no-conditional-expect | warn | - | 禁止在条件语句中使用 expect |
| playwright/no-conditional-in-test | warn | - | 禁止在测试块中使用条件语句 |
| playwright/no-element-handle | warn | - | 禁止使用已弃用的 ElementHandle(推荐使用 Locator) |
| playwright/no-eval | warn | - | 禁止在 Playwright 脚本中使用 evaluate() 执行不安全代码 |
| playwright/no-focused-test | error | - | 禁止提交 focused 测试(如 test.only()) |
| playwright/no-force-option | warn | - | 禁止使用 { force: true } 强制操作选项 |
| playwright/no-nested-step | warn | - | 禁止在 test.step() 中嵌套使用 test.step() |
| playwright/no-networkidle | error | - | 禁止使用不可靠的 networkidle 导航选项 |
| playwright/no-page-pause | warn | - | 禁止提交调试用的 page.pause() 调用 |
| playwright/no-skipped-test | warn | - | 禁止提交跳过的测试(如 test.skip()) |
| playwright/no-standalone-expect | error | - | 禁止在测试块/钩子函数外使用 expect |
| playwright/no-unsafe-references | error | - | 禁止跨测试文件共享不安全引用(如 page 对象) |
| playwright/no-useless-await | warn | - | 禁止对非异步操作使用冗余的 await |
| playwright/no-useless-not | warn | - | 禁止使用冗余的 .not 断言修饰符 |
| playwright/no-wait-for-selector | warn | - | 禁止使用 page.waitForSelector(推荐使用 locator.waitFor()) |
| playwright/no-wait-for-timeout | warn | - | 禁止使用固定的 page.waitForTimeout(推荐使用事件驱动等待) |
| playwright/prefer-web-first-assertions | error | - | 强制使用 Web 优先断言(如 expect(locator).toHaveText()) |
| playwright/valid-describe-callback | error | - | 强制 describe 回调函数的正确用法 |
| playwright/valid-expect | error | - | 强制 expect 调用的有效性 |
| playwright/valid-expect-in-promise | error | - | 确保 Promise 中的 expect 被正确处理 |
| playwright/valid-title | error | - | 强制测试标题符合指定格式要求 |