From 1b2b9c1695a6dec1088daf0b44749100989226a4 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 15 May 2022 09:26:44 +1200 Subject: [PATCH] fix: use the last reference definition when checking jest fn scope (#1109) * fix: use the last reference definition when checking jest fn scope * test: add case with interface + function merging --- src/rules/__tests__/utils.test.ts | 56 ++++++++++++++++++++++++++++++- src/rules/utils.ts | 9 +---- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/rules/__tests__/utils.test.ts b/src/rules/__tests__/utils.test.ts index b11c17e08..e99977c59 100644 --- a/src/rules/__tests__/utils.test.ts +++ b/src/rules/__tests__/utils.test.ts @@ -989,7 +989,61 @@ describe('reference checking', () => { parser: require.resolve('@typescript-eslint/parser'), parserOptions: { sourceType: 'module' }, }, + { + code: dedent` + function it(message: string, fn: () => void): void; + function it(cases: unknown[], message: string, fn: () => void): void; + function it(...all: any[]): void {} + + it('is not a jest function', () => {}); + `, + parser: require.resolve('@typescript-eslint/parser'), + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + interface it {} + function it(...all: any[]): void {} + + it('is not a jest function', () => {}); + `, + parser: require.resolve('@typescript-eslint/parser'), + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + import { it } from '@jest/globals'; + import { it } from '../it-utils'; + + it('is not a jest function', () => {}); + `, + parser: require.resolve('@typescript-eslint/parser'), + parserOptions: { sourceType: 'module' }, + }, + ], + invalid: [ + { + code: dedent` + import { it } from '../it-utils'; + import { it } from '@jest/globals'; + + it('is a jest function', () => {}); + `, + parser: require.resolve('@typescript-eslint/parser'), + parserOptions: { sourceType: 'module' }, + errors: [ + { + messageId: 'details' as const, + data: { + callType: 'test', + numOfArgs: 2, + nodeName: 'it', + }, + column: 1, + line: 4, + }, + ], + }, ], - invalid: [], }); }); diff --git a/src/rules/utils.ts b/src/rules/utils.ts index a024c4cb6..e2ca51217 100644 --- a/src/rules/utils.ts +++ b/src/rules/utils.ts @@ -893,14 +893,7 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => { continue; } - /* istanbul ignore if */ - if (ref.defs.length > 1) { - throw new Error( - `Reference unexpected had more than one definition - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`, - ); - } - - const [def] = ref.defs; + const def = ref.defs[ref.defs.length - 1]; const importDetails = describePossibleImportDef(def);