Skip to content

Commit

Permalink
fix(valid-expect): Don' mark expect.anything() as invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Mar 22, 2024
1 parent bf821f5 commit a9045db
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/rules/prefer-comparison-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Rule } from 'eslint'
import * as ESTree from 'estree'
import {
equalityMatchers,
findParent,
getParent,
getRawValue,
getStringValue,
Expand Down Expand Up @@ -48,8 +47,8 @@ export default {
const call = parseFnCall(context, node)
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return

const expect = findParent(call.head.node, 'CallExpression')
if (!expect) return
const expect = getParent(call.head.node)
if (expect?.type !== 'CallExpression') return

const [comparison] = expect.arguments
const expectCallEnd = expect.range![1]
Expand Down
5 changes: 2 additions & 3 deletions src/rules/prefer-equality-matcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Rule } from 'eslint'
import {
equalityMatchers,
findParent,
getParent,
getRawValue,
getStringValue,
Expand All @@ -16,8 +15,8 @@ export default {
const call = parseFnCall(context, node)
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return

const expect = findParent(call.head.node, 'CallExpression')
if (!expect) return
const expect = getParent(call.head.node)
if (expect?.type !== 'CallExpression') return

const [comparison] = expect.arguments
const expectCallEnd = expect.range![1]
Expand Down
6 changes: 3 additions & 3 deletions src/rules/prefer-to-contain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Rule } from 'eslint'
import ESTree from 'estree'
import {
equalityMatchers,
findParent,
getParent,
getStringValue,
isBooleanLiteral,
isPropertyAccessor,
Expand All @@ -28,8 +28,8 @@ export default {
const call = parseFnCall(context, node)
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return

const expect = findParent(call.head.node, 'CallExpression')
if (!expect) return
const expect = getParent(call.head.node)
if (expect?.type !== 'CallExpression') return

const [includesCall] = expect.arguments
const { matcher } = call
Expand Down
29 changes: 16 additions & 13 deletions src/rules/valid-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,22 @@ runRuleTester('valid-expect', rule, {
},
],
valid: [
'expectPayButtonToBeEnabled()',
'expect("something").toBe("else")',
'softExpect("something").toBe("else")',
'expect.soft("something").toBe("else")',
'expect.poll(() => "something").toBe("else")',
'expect(true).toBeDefined()',
'expect(undefined).not.toBeDefined()',
'expect([1, 2, 3]).toEqual([1, 2, 3])',
'expect(1, "1 !== 2").toBe(2)',
'expect.soft(1, "1 !== 2").toBe(2)',
'expect["soft"](1, "1 !== 2")["toBe"](2)',
'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)',
'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)',
{ code: 'expectPayButtonToBeEnabled()' },
{ code: 'expect("something").toBe("else")' },
{ code: 'expect("something").toBe(expect.anything())' },
{ code: 'expect("something").toEqual({ foo: expect.anything() })' },
{ code: 'expect("something").toBe(expect.arrayContaining([1, 2, 3]))' },
{ code: 'softExpect("something").toBe("else")' },
{ code: 'expect.soft("something").toBe("else")' },
{ code: 'expect.poll(() => "something").toBe("else")' },
{ code: 'expect(true).toBeDefined()' },
{ code: 'expect(undefined).not.toBeDefined()' },
{ code: 'expect([1, 2, 3]).toEqual([1, 2, 3])' },
{ code: 'expect(1, "1 !== 2").toBe(2)' },
{ code: 'expect.soft(1, "1 !== 2").toBe(2)' },
{ code: 'expect["soft"](1, "1 !== 2")["toBe"](2)' },
{ code: 'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)' },
{ code: 'expect[`poll`](() => 1, { message: "1 !== 2" })[`toBe`](2)' },
// minArgs
{
code: 'expect(1, "1 !== 2").toBe(2)',
Expand Down
6 changes: 3 additions & 3 deletions src/rules/valid-expect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule } from 'eslint'
import * as ESTree from 'estree'
import { findParent, getParent, getStringValue } from '../utils/ast'
import { getParent, getStringValue } from '../utils/ast'
import { getAmountData } from '../utils/misc'
import {
isSupportedAccessor,
Expand Down Expand Up @@ -81,8 +81,8 @@ export default {
return
}

const expect = findParent(call.head.node, 'CallExpression')
if (!expect) return
const expect = getParent(call.head.node)
if (expect?.type !== 'CallExpression') return

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

0 comments on commit a9045db

Please sign in to comment.