Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prefer-expect-assertions]fix false negatives for test context usage #600

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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