-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #296
- Loading branch information
Showing
5 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Disallow usage of the `.slow` annotation (`no-slowed-test`) | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```javascript | ||
test.slow('slow this test', async ({ page }) => {}) | ||
|
||
test.describe('slow test inside describe', () => { | ||
test.slow() | ||
}) | ||
|
||
test.describe('slow test conditionally', async ({ browserName }) => { | ||
test.slow(browserName === 'firefox', 'Working on it') | ||
}) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```javascript | ||
test('this test', async ({ page }) => {}) | ||
|
||
test.describe('two tests', () => { | ||
test('one', async ({ page }) => {}) | ||
test('two', async ({ page }) => {}) | ||
}) | ||
``` | ||
|
||
## Options | ||
|
||
```json | ||
{ | ||
"playwright/no-slowed-test": [ | ||
"error", | ||
{ | ||
"allowConditional": false | ||
} | ||
] | ||
} | ||
``` | ||
|
||
### `allowConditional` | ||
|
||
Setting this option to `true` will allow using `test.slow()` to conditionally | ||
mark a test as slow. This can be helpful if you want to prevent usage of | ||
`test.slow` being added by mistake but still allow slow tests based on | ||
browser/environment setup. | ||
|
||
Example of **correct** code for the `{ "allowConditional": true }` option: | ||
|
||
```javascript | ||
test('foo', ({ browserName }) => { | ||
test.slow(browserName === 'firefox', 'Still working on it') | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import { runRuleTester } from '../utils/rule-tester' | ||
import rule from './no-slowed-test' | ||
|
||
const messageId = 'removeSlowedTestAnnotation' | ||
|
||
runRuleTester('no-slowed-test', rule, { | ||
invalid: [ | ||
{ | ||
code: 'test.slow("slow this test", async ({ page }) => {});', | ||
errors: [ | ||
{ | ||
column: 6, | ||
endColumn: 10, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [ | ||
{ | ||
messageId, | ||
output: 'test("slow this test", async ({ page }) => {});', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test["slow"]("slow this test", async ({ page }) => {});', | ||
errors: [ | ||
{ | ||
column: 6, | ||
endColumn: 12, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [ | ||
{ | ||
messageId, | ||
output: 'test("slow this test", async ({ page }) => {});', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test[`slow`]("slow this test", async ({ page }) => {});', | ||
errors: [ | ||
{ | ||
column: 6, | ||
endColumn: 12, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [ | ||
{ | ||
messageId, | ||
output: 'test("slow this test", async ({ page }) => {});', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test.slow("a test", { tag: ["@fast", "@login"] }, () => {})', | ||
errors: [ | ||
{ | ||
column: 6, | ||
endColumn: 10, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [ | ||
{ | ||
messageId, | ||
output: 'test("a test", { tag: ["@fast", "@login"] }, () => {})', | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test.slow(browserName === "firefox");', | ||
errors: [ | ||
{ | ||
column: 1, | ||
endColumn: 37, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [{ messageId, output: '' }], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test.slow(browserName === "firefox", "Still working on it");', | ||
errors: [ | ||
{ | ||
column: 1, | ||
endColumn: 60, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [{ messageId, output: '' }], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test.slow()', | ||
errors: [ | ||
{ | ||
column: 1, | ||
endColumn: 12, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [{ messageId, output: '' }], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test["slow"]()', | ||
errors: [ | ||
{ | ||
column: 1, | ||
endColumn: 15, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [{ messageId, output: '' }], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'test[`slow`]()', | ||
errors: [ | ||
{ | ||
column: 1, | ||
endColumn: 15, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [{ messageId, output: '' }], | ||
}, | ||
], | ||
}, | ||
// Global aliases | ||
{ | ||
code: 'it.slow("slow this test", async ({ page }) => {});', | ||
errors: [ | ||
{ | ||
column: 4, | ||
endColumn: 8, | ||
line: 1, | ||
messageId: 'noSlowedTest', | ||
suggestions: [ | ||
{ | ||
messageId, | ||
output: 'it("slow this test", async ({ page }) => {});', | ||
}, | ||
], | ||
}, | ||
], | ||
settings: { | ||
playwright: { | ||
globalAliases: { test: ['it'] }, | ||
}, | ||
}, | ||
}, | ||
], | ||
valid: [ | ||
'test("a test", () => {});', | ||
'test("a test", { tag: "@fast" }, () => {});', | ||
'test("a test", { tag: ["@fast", "@report"] }, () => {});', | ||
'test("one", async ({ page }) => {});', | ||
'test.only(isMobile, "Settings page does not work in mobile yet");', | ||
'test.skip();', | ||
'test["skip"]();', | ||
'test[`skip`]();', | ||
'const slow = true;', | ||
'function slow() { return null };', | ||
'this.slow();', | ||
'this["slow"]();', | ||
'this[`slow`]();', | ||
{ | ||
code: 'test.slow(browserName === "firefox", "Still working on it");', | ||
options: [{ allowConditional: true }], | ||
}, | ||
// Global aliases | ||
{ | ||
code: 'it("a test", () => {});', | ||
settings: { | ||
playwright: { | ||
globalAliases: { test: ['it'] }, | ||
}, | ||
}, | ||
}, | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { getStringValue } from '../utils/ast' | ||
import { createRule } from '../utils/createRule' | ||
import { parseFnCall } from '../utils/parseFnCall' | ||
|
||
export default createRule({ | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const options = context.options[0] || {} | ||
const allowConditional = !!options.allowConditional | ||
|
||
const call = parseFnCall(context, node) | ||
if (call?.group !== 'test') { | ||
return | ||
} | ||
|
||
const slowNode = call.members.find((s) => getStringValue(s) === 'slow') | ||
if (!slowNode) return | ||
|
||
// If the call is a standalone `test.slow()` call, and not a test | ||
// annotation, we have to treat it a bit differently. | ||
const isStandalone = call.type === 'config' | ||
|
||
// If allowConditional is enabled and it's not a test function, | ||
// we ignore any `test.slow` calls that have no arguments. | ||
if (isStandalone && allowConditional) { | ||
return | ||
} | ||
|
||
context.report({ | ||
messageId: 'noSlowedTest', | ||
node: isStandalone ? node : slowNode, | ||
suggest: [ | ||
{ | ||
fix: (fixer) => { | ||
return isStandalone | ||
? fixer.remove(node.parent) | ||
: fixer.removeRange([ | ||
slowNode.range![0] - 1, | ||
slowNode.range![1] + | ||
Number(slowNode.type !== 'Identifier'), | ||
]) | ||
}, | ||
messageId: 'removeSlowedTestAnnotation', | ||
}, | ||
], | ||
}) | ||
}, | ||
} | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Prevent usage of the `.slow()` slow test annotation.', | ||
recommended: true, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/no-slowed-test.md', | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
noSlowedTest: 'Unexpected use of the `.slow()` annotation.', | ||
removeSlowedTestAnnotation: 'Remove the `.slow()` annotation.', | ||
}, | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
properties: { | ||
allowConditional: { | ||
default: false, | ||
type: 'boolean', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
type: 'suggestion', | ||
}, | ||
}) |