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

fix: vitest/expect-expect throws false positive #604

Merged
merged 3 commits into from
Dec 19, 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
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