Skip to content

Commit

Permalink
fix: vitest/expect-expect throws false positive (#604)
Browse files Browse the repository at this point in the history
* fix: expect-expect throws false positive

* test: add test

* test: add test in no-disabled-tests
  • Loading branch information
y-hsgw authored Dec 19, 2024
1 parent 085e320 commit 7fd75ea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/utils/parse-vitest-fn-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ const resolvePossibleAliasedGlobal = (
return null
}

const isAncestorTestCaseCall = ({ parent }: TSESTree.Node) => {
if (parent?.type === AST_NODE_TYPES.CallExpression && parent.callee.type === AST_NODE_TYPES.Identifier)
return TestCaseName.hasOwnProperty(parent.callee.name)
}

export const resolveScope = (
scope: TSESLint.Scope.Scope,
identifier: string
Expand All @@ -383,7 +388,7 @@ export const resolveScope = (
}

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

const importDetails = describePossibleImportDef(def)
Expand Down
8 changes: 7 additions & 1 deletion tests/expect-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ const it = base.extend<{
async bar({}, use) {
await use('bar')
}
})`
})`,
`
it('example', async () => {
const result = Promise.reject<string>('error');
await expect(result.then((it) => it.toUpperCase())).rejects.toThrow();
});`
],
invalid: [
{
Expand Down
11 changes: 10 additions & 1 deletion tests/no-disabled-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ ruleTester.run(RULE_NAME, rule, {
})`,
` import { test } from './test-utils';
test('something');`
test('something');`,
`test("foo", () => {
const upper1 = (x: string) => x.toUpperCase();
const upper2 = (y: string) => y.toUpperCase();
const upper3 = (xy: string) => xy.toUpperCase();
const upper4 = (yx: string) => yx.toUpperCase();
const upper5 = (a: string) => a.toUpperCase();
const length = (x: string) => x.length;
expect("test").toBe('test');
});`
],
invalid: [
{
Expand Down

0 comments on commit 7fd75ea

Please sign in to comment.