From 7fd75eaee2e2670031ef1cc6a85ed4a83d940460 Mon Sep 17 00:00:00 2001 From: Yukihiro Hasegawa <49516827+y-hsgw@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:06:33 +0900 Subject: [PATCH] fix: vitest/expect-expect throws false positive (#604) * fix: expect-expect throws false positive * test: add test * test: add test in no-disabled-tests --- src/utils/parse-vitest-fn-call.ts | 7 ++++++- tests/expect-expect.test.ts | 8 +++++++- tests/no-disabled-tests.test.ts | 11 ++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/utils/parse-vitest-fn-call.ts b/src/utils/parse-vitest-fn-call.ts index 0d43b79..a8a6e7d 100644 --- a/src/utils/parse-vitest-fn-call.ts +++ b/src/utils/parse-vitest-fn-call.ts @@ -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 @@ -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) diff --git a/tests/expect-expect.test.ts b/tests/expect-expect.test.ts index f3f7fef..39a7e1d 100644 --- a/tests/expect-expect.test.ts +++ b/tests/expect-expect.test.ts @@ -36,7 +36,13 @@ const it = base.extend<{ async bar({}, use) { await use('bar') } -})` +})`, + ` +it('example', async () => { + const result = Promise.reject('error'); + + await expect(result.then((it) => it.toUpperCase())).rejects.toThrow(); +});` ], invalid: [ { diff --git a/tests/no-disabled-tests.test.ts b/tests/no-disabled-tests.test.ts index 459a2e4..a49e2b2 100644 --- a/tests/no-disabled-tests.test.ts +++ b/tests/no-disabled-tests.test.ts @@ -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: [ {