Skip to content

Commit a9045db

Browse files
committed
fix(valid-expect): Don' mark expect.anything() as invalid.
1 parent bf821f5 commit a9045db

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

src/rules/prefer-comparison-matcher.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Rule } from 'eslint'
22
import * as ESTree from 'estree'
33
import {
44
equalityMatchers,
5-
findParent,
65
getParent,
76
getRawValue,
87
getStringValue,
@@ -48,8 +47,8 @@ export default {
4847
const call = parseFnCall(context, node)
4948
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
5049

51-
const expect = findParent(call.head.node, 'CallExpression')
52-
if (!expect) return
50+
const expect = getParent(call.head.node)
51+
if (expect?.type !== 'CallExpression') return
5352

5453
const [comparison] = expect.arguments
5554
const expectCallEnd = expect.range![1]

src/rules/prefer-equality-matcher.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Rule } from 'eslint'
22
import {
33
equalityMatchers,
4-
findParent,
54
getParent,
65
getRawValue,
76
getStringValue,
@@ -16,8 +15,8 @@ export default {
1615
const call = parseFnCall(context, node)
1716
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
1817

19-
const expect = findParent(call.head.node, 'CallExpression')
20-
if (!expect) return
18+
const expect = getParent(call.head.node)
19+
if (expect?.type !== 'CallExpression') return
2120

2221
const [comparison] = expect.arguments
2322
const expectCallEnd = expect.range![1]

src/rules/prefer-to-contain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Rule } from 'eslint'
22
import ESTree from 'estree'
33
import {
44
equalityMatchers,
5-
findParent,
5+
getParent,
66
getStringValue,
77
isBooleanLiteral,
88
isPropertyAccessor,
@@ -28,8 +28,8 @@ export default {
2828
const call = parseFnCall(context, node)
2929
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
3030

31-
const expect = findParent(call.head.node, 'CallExpression')
32-
if (!expect) return
31+
const expect = getParent(call.head.node)
32+
if (expect?.type !== 'CallExpression') return
3333

3434
const [includesCall] = expect.arguments
3535
const { matcher } = call

src/rules/valid-expect.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,22 @@ runRuleTester('valid-expect', rule, {
213213
},
214214
],
215215
valid: [
216-
'expectPayButtonToBeEnabled()',
217-
'expect("something").toBe("else")',
218-
'softExpect("something").toBe("else")',
219-
'expect.soft("something").toBe("else")',
220-
'expect.poll(() => "something").toBe("else")',
221-
'expect(true).toBeDefined()',
222-
'expect(undefined).not.toBeDefined()',
223-
'expect([1, 2, 3]).toEqual([1, 2, 3])',
224-
'expect(1, "1 !== 2").toBe(2)',
225-
'expect.soft(1, "1 !== 2").toBe(2)',
226-
'expect["soft"](1, "1 !== 2")["toBe"](2)',
227-
'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)',
228-
'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)',
216+
{ code: 'expectPayButtonToBeEnabled()' },
217+
{ code: 'expect("something").toBe("else")' },
218+
{ code: 'expect("something").toBe(expect.anything())' },
219+
{ code: 'expect("something").toEqual({ foo: expect.anything() })' },
220+
{ code: 'expect("something").toBe(expect.arrayContaining([1, 2, 3]))' },
221+
{ code: 'softExpect("something").toBe("else")' },
222+
{ code: 'expect.soft("something").toBe("else")' },
223+
{ code: 'expect.poll(() => "something").toBe("else")' },
224+
{ code: 'expect(true).toBeDefined()' },
225+
{ code: 'expect(undefined).not.toBeDefined()' },
226+
{ code: 'expect([1, 2, 3]).toEqual([1, 2, 3])' },
227+
{ code: 'expect(1, "1 !== 2").toBe(2)' },
228+
{ code: 'expect.soft(1, "1 !== 2").toBe(2)' },
229+
{ code: 'expect["soft"](1, "1 !== 2")["toBe"](2)' },
230+
{ code: 'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)' },
231+
{ code: 'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)' },
229232
// minArgs
230233
{
231234
code: 'expect(1, "1 !== 2").toBe(2)',

src/rules/valid-expect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Rule } from 'eslint'
22
import * as ESTree from 'estree'
3-
import { findParent, getParent, getStringValue } from '../utils/ast'
3+
import { getParent, getStringValue } from '../utils/ast'
44
import { getAmountData } from '../utils/misc'
55
import {
66
isSupportedAccessor,
@@ -81,8 +81,8 @@ export default {
8181
return
8282
}
8383

84-
const expect = findParent(call.head.node, 'CallExpression')
85-
if (!expect) return
84+
const expect = getParent(call.head.node)
85+
if (expect?.type !== 'CallExpression') return
8686

8787
if (expect.arguments.length < minArgs) {
8888
const expectLength = getStringValue(call.head.node).length

0 commit comments

Comments
 (0)