跳到主要内容

Playwright 规则

eslint-plugin-playwright 插件是为使用Playwright进行 E2E 测试的项目而设计的。它提供了 Playwright 官方推荐的规则,确保 Playwright 测试代码符合最佳实践,从而提高测试的可靠性和可读性。

// .eslintrc.js

module.exports = {
extends: [
'plugin:playwright/recommended',
],
};
注意

在编写 Playwright 代码时,可以适当关闭一些规则,避免过于严格的检查影响开发效率。但是,仍然鼓励遵循这些规范,以提高代码质量和可维护性。

规则名称错误级别配置选项描述
no-empty-patternoff-禁止对象/数组解构中使用空模式(ESLint 核心规则)
playwright/expect-expectwarn-强制在测试中至少有一个 expect 断言
playwright/max-nested-describewarn-限制 describe 块的嵌套层级深度
playwright/missing-playwright-awaiterror-强制对 Playwright 异步操作使用 await
playwright/no-conditional-expectwarn-禁止在条件语句中使用 expect
playwright/no-conditional-in-testwarn-禁止在测试块中使用条件语句
playwright/no-element-handlewarn-禁止使用已弃用的 ElementHandle(推荐使用 Locator
playwright/no-evalwarn-禁止在 Playwright 脚本中使用 evaluate() 执行不安全代码
playwright/no-focused-testerror-禁止提交 focused 测试(如 test.only()
playwright/no-force-optionwarn-禁止使用 { force: true } 强制操作选项
playwright/no-nested-stepwarn-禁止在 test.step() 中嵌套使用 test.step()
playwright/no-networkidleerror-禁止使用不可靠的 networkidle 导航选项
playwright/no-page-pausewarn-禁止提交调试用的 page.pause() 调用
playwright/no-skipped-testwarn-禁止提交跳过的测试(如 test.skip()
playwright/no-standalone-expecterror-禁止在测试块/钩子函数外使用 expect
playwright/no-unsafe-referenceserror-禁止跨测试文件共享不安全引用(如 page 对象)
playwright/no-useless-awaitwarn-禁止对非异步操作使用冗余的 await
playwright/no-useless-notwarn-禁止使用冗余的 .not 断言修饰符
playwright/no-wait-for-selectorwarn-禁止使用 page.waitForSelector(推荐使用 locator.waitFor()
playwright/no-wait-for-timeoutwarn-禁止使用固定的 page.waitForTimeout(推荐使用事件驱动等待)
playwright/prefer-web-first-assertionserror-强制使用 Web 优先断言(如 expect(locator).toHaveText()
playwright/valid-describe-callbackerror-强制 describe 回调函数的正确用法
playwright/valid-expecterror-强制 expect 调用的有效性
playwright/valid-expect-in-promiseerror-确保 Promise 中的 expect 被正确处理
playwright/valid-titleerror-强制测试标题符合指定格式要求