Skip to content

Commit 85d8e48

Browse files
authored
Fix false positive when using allowConditional (#379)
* Fix false positive when using `allowConditional` * Sync
1 parent 917aced commit 85d8e48

File tree

7 files changed

+122
-7
lines changed

7 files changed

+122
-7
lines changed

docs/rules/no-skipped-test.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,24 @@ Setting this option to `true` will allow using `test.skip()` to
5252
This can be helpful if you want to prevent usage of `test.skip` being added by
5353
mistake but still allow conditional tests based on browser/environment setup.
5454

55+
Examples of **incorrect** code for the `{ "allowConditional": true }` option:
56+
57+
```javascript
58+
test.skip('foo', ({}) => {
59+
expect(1).toBe(1)
60+
})
61+
62+
test('foo', ({}) => {
63+
test.skip()
64+
expect(1).toBe(1)
65+
})
66+
```
67+
5568
Example of **correct** code for the `{ "allowConditional": true }` option:
5669

5770
```javascript
5871
test('foo', ({ browserName }) => {
5972
test.skip(browserName === 'firefox', 'Still working on it')
73+
expect(1).toBe(1)
6074
})
6175
```

docs/rules/no-slowed-test.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,24 @@ mark a test as slow. This can be helpful if you want to prevent usage of
4747
`test.slow` being added by mistake but still allow slow tests based on
4848
browser/environment setup.
4949

50+
Examples of **incorrect** code for the `{ "allowConditional": true }` option:
51+
52+
```javascript
53+
test.slow('foo', ({}) => {
54+
expect(1).toBe(1)
55+
})
56+
57+
test('foo', ({}) => {
58+
test.slow()
59+
expect(1).toBe(1)
60+
})
61+
```
62+
5063
Example of **correct** code for the `{ "allowConditional": true }` option:
5164

5265
```javascript
5366
test('foo', ({ browserName }) => {
5467
test.slow(browserName === 'firefox', 'Still working on it')
68+
expect(1).toBe(1)
5569
})
5670
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"lint": "eslint .",
3636
"fmt": "prettier --write .",
3737
"fmt:check": "prettier --check .",
38-
"test": "vitest --run",
38+
"test": "vitest --run --hideSkippedTests",
3939
"test:watch": "vitest --reporter=dot --run",
4040
"ts": "tsc --noEmit"
4141
},

src/rules/no-skipped-test.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,37 @@ runRuleTester('no-skipped-test', rule, {
303303
},
304304
},
305305
},
306+
{
307+
code: 'test("foo", ({}) => { test.skip(); })',
308+
errors: [
309+
{
310+
column: 23,
311+
endColumn: 34,
312+
line: 1,
313+
messageId: 'noSkippedTest',
314+
suggestions: [{ messageId, output: 'test("foo", ({}) => { })' }],
315+
},
316+
],
317+
options: [{ allowConditional: true }],
318+
},
319+
{
320+
code: 'test.skip("foo", ({}) => { expect(1).toBe(1) })',
321+
errors: [
322+
{
323+
column: 6,
324+
endColumn: 10,
325+
line: 1,
326+
messageId: 'noSkippedTest',
327+
suggestions: [
328+
{
329+
messageId,
330+
output: 'test("foo", ({}) => { })',
331+
},
332+
],
333+
},
334+
],
335+
options: [{ allowConditional: true }],
336+
},
306337
],
307338
valid: [
308339
'test("a test", () => {});',
@@ -323,7 +354,15 @@ runRuleTester('no-skipped-test', rule, {
323354
'this["skip"]();',
324355
'this[`skip`]();',
325356
{
326-
code: 'test.skip(browserName === "firefox", "Still working on it");',
357+
code: 'test("foo", ({ browserName }) => { test.skip(browserName === "firefox", "Still working on it") })',
358+
options: [{ allowConditional: true }],
359+
},
360+
{
361+
code: 'test("foo", ({ browserName }) => { if (browserName === "firefox") { test.skip("Still working on it") } })',
362+
options: [{ allowConditional: true }],
363+
},
364+
{
365+
code: 'test("foo", ({ browserName }) => { if (browserName === "firefox") { test.skip() } })',
327366
options: [{ allowConditional: true }],
328367
},
329368
// Global aliases

src/rules/no-skipped-test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getStringValue } from '../utils/ast.js'
1+
import { findParent, getStringValue } from '../utils/ast.js'
22
import { createRule } from '../utils/createRule.js'
33
import { parseFnCall } from '../utils/parseFnCall.js'
44

@@ -27,7 +27,12 @@ export default createRule({
2727

2828
// If allowConditional is enabled and it's not a test/describe function,
2929
// we ignore any `test.skip` calls that have no arguments.
30-
if (isStandalone && allowConditional) {
30+
if (
31+
isStandalone &&
32+
allowConditional &&
33+
(node.arguments.length !== 0 ||
34+
findParent(node, 'BlockStatement')?.parent?.type === 'IfStatement')
35+
) {
3136
return
3237
}
3338

src/rules/no-slowed-test.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ runRuleTester('no-slowed-test', rule, {
133133
},
134134
],
135135
},
136+
{
137+
code: 'test.slow("foo", ({}) => { expect(1).toBe(1) })',
138+
errors: [
139+
{
140+
column: 6,
141+
endColumn: 10,
142+
line: 1,
143+
messageId: 'noSlowedTest',
144+
suggestions: [
145+
{
146+
messageId,
147+
output: 'test("foo", ({}) => { expect(1).toBe(1) })',
148+
},
149+
],
150+
},
151+
],
152+
},
136153
// Global aliases
137154
{
138155
code: 'it.slow("slow this test", async ({ page }) => {});',
@@ -156,6 +173,19 @@ runRuleTester('no-slowed-test', rule, {
156173
},
157174
},
158175
},
176+
{
177+
code: 'test("foo", ({}) => { test.slow(); })',
178+
errors: [
179+
{
180+
column: 23,
181+
endColumn: 34,
182+
line: 1,
183+
messageId: 'noSlowedTest',
184+
suggestions: [{ messageId, output: 'test("foo", ({}) => { })' }],
185+
},
186+
],
187+
options: [{ allowConditional: true }],
188+
},
159189
],
160190
valid: [
161191
'test("a test", () => {});',
@@ -172,7 +202,15 @@ runRuleTester('no-slowed-test', rule, {
172202
'this["slow"]();',
173203
'this[`slow`]();',
174204
{
175-
code: 'test.slow(browserName === "firefox", "Still working on it");',
205+
code: 'test("foo", ({ browserName }) => { test.slow(browserName === "firefox", "Still working on it") })',
206+
options: [{ allowConditional: true }],
207+
},
208+
{
209+
code: 'test("foo", ({ browserName }) => { if (browserName === "firefox") { test.slow("Still working on it") } })',
210+
options: [{ allowConditional: true }],
211+
},
212+
{
213+
code: 'test("foo", ({ browserName }) => { if (browserName === "firefox") { test.slow() } })',
176214
options: [{ allowConditional: true }],
177215
},
178216
// Global aliases

src/rules/no-slowed-test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getStringValue } from '../utils/ast.js'
1+
import { findParent, getStringValue } from '../utils/ast.js'
22
import { createRule } from '../utils/createRule.js'
33
import { parseFnCall } from '../utils/parseFnCall.js'
44

@@ -23,7 +23,12 @@ export default createRule({
2323

2424
// If allowConditional is enabled and it's not a test function,
2525
// we ignore any `test.slow` calls that have no arguments.
26-
if (isStandalone && allowConditional) {
26+
if (
27+
isStandalone &&
28+
allowConditional &&
29+
(node.arguments.length !== 0 ||
30+
findParent(node, 'BlockStatement')?.parent?.type === 'IfStatement')
31+
) {
2732
return
2833
}
2934

0 commit comments

Comments
 (0)