Skip to content

Commit 1b2b9c1

Browse files
authored
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
1 parent 8c6a856 commit 1b2b9c1

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

src/rules/__tests__/utils.test.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,61 @@ describe('reference checking', () => {
989989
parser: require.resolve('@typescript-eslint/parser'),
990990
parserOptions: { sourceType: 'module' },
991991
},
992+
{
993+
code: dedent`
994+
function it(message: string, fn: () => void): void;
995+
function it(cases: unknown[], message: string, fn: () => void): void;
996+
function it(...all: any[]): void {}
997+
998+
it('is not a jest function', () => {});
999+
`,
1000+
parser: require.resolve('@typescript-eslint/parser'),
1001+
parserOptions: { sourceType: 'module' },
1002+
},
1003+
{
1004+
code: dedent`
1005+
interface it {}
1006+
function it(...all: any[]): void {}
1007+
1008+
it('is not a jest function', () => {});
1009+
`,
1010+
parser: require.resolve('@typescript-eslint/parser'),
1011+
parserOptions: { sourceType: 'module' },
1012+
},
1013+
{
1014+
code: dedent`
1015+
import { it } from '@jest/globals';
1016+
import { it } from '../it-utils';
1017+
1018+
it('is not a jest function', () => {});
1019+
`,
1020+
parser: require.resolve('@typescript-eslint/parser'),
1021+
parserOptions: { sourceType: 'module' },
1022+
},
1023+
],
1024+
invalid: [
1025+
{
1026+
code: dedent`
1027+
import { it } from '../it-utils';
1028+
import { it } from '@jest/globals';
1029+
1030+
it('is a jest function', () => {});
1031+
`,
1032+
parser: require.resolve('@typescript-eslint/parser'),
1033+
parserOptions: { sourceType: 'module' },
1034+
errors: [
1035+
{
1036+
messageId: 'details' as const,
1037+
data: {
1038+
callType: 'test',
1039+
numOfArgs: 2,
1040+
nodeName: 'it',
1041+
},
1042+
column: 1,
1043+
line: 4,
1044+
},
1045+
],
1046+
},
9921047
],
993-
invalid: [],
9941048
});
9951049
});

src/rules/utils.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -893,14 +893,7 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => {
893893
continue;
894894
}
895895

896-
/* istanbul ignore if */
897-
if (ref.defs.length > 1) {
898-
throw new Error(
899-
`Reference unexpected had more than one definition - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
900-
);
901-
}
902-
903-
const [def] = ref.defs;
896+
const def = ref.defs[ref.defs.length - 1];
904897

905898
const importDetails = describePossibleImportDef(def);
906899

0 commit comments

Comments
 (0)