diff --git a/src/rules/prefer-comparison-matcher.ts b/src/rules/prefer-comparison-matcher.ts index 00c0714..e07cffa 100644 --- a/src/rules/prefer-comparison-matcher.ts +++ b/src/rules/prefer-comparison-matcher.ts @@ -2,7 +2,6 @@ import { Rule } from 'eslint' import * as ESTree from 'estree' import { equalityMatchers, - findParent, getParent, getRawValue, getStringValue, @@ -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] diff --git a/src/rules/prefer-equality-matcher.ts b/src/rules/prefer-equality-matcher.ts index 63fc629..78fc082 100644 --- a/src/rules/prefer-equality-matcher.ts +++ b/src/rules/prefer-equality-matcher.ts @@ -1,7 +1,6 @@ import { Rule } from 'eslint' import { equalityMatchers, - findParent, getParent, getRawValue, getStringValue, @@ -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] diff --git a/src/rules/prefer-to-contain.ts b/src/rules/prefer-to-contain.ts index a0c4247..48c7da2 100644 --- a/src/rules/prefer-to-contain.ts +++ b/src/rules/prefer-to-contain.ts @@ -2,7 +2,7 @@ import { Rule } from 'eslint' import ESTree from 'estree' import { equalityMatchers, - findParent, + getParent, getStringValue, isBooleanLiteral, isPropertyAccessor, @@ -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 diff --git a/src/rules/valid-expect.test.ts b/src/rules/valid-expect.test.ts index c458b81..7d6ef31 100644 --- a/src/rules/valid-expect.test.ts +++ b/src/rules/valid-expect.test.ts @@ -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)', diff --git a/src/rules/valid-expect.ts b/src/rules/valid-expect.ts index 5f7d243..5163ebd 100644 --- a/src/rules/valid-expect.ts +++ b/src/rules/valid-expect.ts @@ -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, @@ -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