Skip to content

Commit

Permalink
[prefer-expect-assertions]fix false negatives for test context usage (#…
Browse files Browse the repository at this point in the history
…600)

* fix: fix false negatives for test context usage

* test: add test
  • Loading branch information
y-hsgw authored Dec 17, 2024
1 parent a0e9641 commit b3303b7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/rules/prefer-expect-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default createEslintRule<Options[], MessageIds>({
let hasExpectInCallBack = false
let hasExpectInLoop = false
let hasExpectAssertAsFirstStatement = false
let testContextName: string | null = null
let inTestCaseCall = false
let inForLoop = false

Expand Down Expand Up @@ -176,6 +177,10 @@ export default createEslintRule<Options[], MessageIds>({
return
}

if (vitestFnCall?.head.type === "testContext" && vitestFnCall.members[0].type === AST_NODE_TYPES.Identifier && vitestFnCall.members[0].name === "expect") {
testContextName = `${vitestFnCall.head.local}`
}

if (vitestFnCall?.type === 'expect' && inTestCaseCall) {
if (expressionDepth === 1 && isFirstStatement(node) && vitestFnCall.head.node.parent?.type === AST_NODE_TYPES.MemberExpression && vitestFnCall.members.length === 1
&& ['assertions', 'hasAssertions'].includes(getAccessorValue(vitestFnCall.members[0]))) {
Expand Down Expand Up @@ -217,8 +222,9 @@ export default createEslintRule<Options[], MessageIds>({
const suggestions: Array<[MessageIds, string]> = []

if (secondArg.body.type === AST_NODE_TYPES.BlockStatement) {
suggestions.push(['suggestAddingHasAssertions', 'expect.hasAssertions();'],
['suggestAddingAssertions', 'expect.assertions();'])
const prefix = testContextName ? `${testContextName}.` : "";
suggestions.push(['suggestAddingHasAssertions', `${prefix}expect.hasAssertions();`],
['suggestAddingAssertions', `${prefix}expect.assertions();`])
}

context.report({
Expand Down
6 changes: 5 additions & 1 deletion src/utils/parse-vitest-fn-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const parseVitestFnCallWithReasonInner = (

const links = [name, ...rest.map(getAccessorValue)]

if (name !== 'vi' && name !== 'expect' && name !== 'expectTypeOf' && !ValidVitestFnCallChains.has(links.join('.')))
if (resolved.type !== "testContext" && name !== 'vi' && name !== 'expect' && name !== 'expectTypeOf' && !ValidVitestFnCallChains.has(links.join('.')))
return null

const parsedVitestFnCall: Omit<ParsedVitestFnCall, 'type'> = {
Expand Down Expand Up @@ -382,6 +382,10 @@ export const resolveScope = (
return "testContext"
}

const namedParam = isFunction(def.node) ? def.node.params.find(params => params.type === AST_NODE_TYPES.Identifier ) : undefined
if (namedParam)
return "testContext"

const importDetails = describePossibleImportDef(def)

if (importDetails?.local === identifier)
Expand Down
41 changes: 41 additions & 0 deletions tests/prefer-expect-assertions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,47 @@ it('my test description', ({ expect }) => {expect.assertions();
expect(sum(a, b)).toBe(a + b);
})
`
}
]
}
],
},
{
code: `
it('my test description', (context) => {
const a = 1;
const b = 2;
context.expect(sum(a, b)).toBe(a + b);
})
`,
errors: [
{
messageId: 'haveExpectAssertions',
column: 1,
line: 2,
suggestions: [
{
messageId: 'suggestAddingHasAssertions',
output: `
it('my test description', (context) => {context.expect.hasAssertions();
const a = 1;
const b = 2;
context.expect(sum(a, b)).toBe(a + b);
})
`
},
{
messageId: 'suggestAddingAssertions',
output: `
it('my test description', (context) => {context.expect.assertions();
const a = 1;
const b = 2;
context.expect(sum(a, b)).toBe(a + b);
})
`
}
]
Expand Down

0 comments on commit b3303b7

Please sign in to comment.