Skip to content

Commit

Permalink
fix: use the last reference definition when checking jest fn scope (#…
Browse files Browse the repository at this point in the history
…1109)

* fix: use the last reference definition when checking jest fn scope

* test: add case with interface + function merging
  • Loading branch information
G-Rath committed May 14, 2022
1 parent 8c6a856 commit 1b2b9c1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
56 changes: 55 additions & 1 deletion src/rules/__tests__/utils.test.ts
Expand Up @@ -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: [],
});
});
9 changes: 1 addition & 8 deletions src/rules/utils.ts
Expand Up @@ -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);

Expand Down

0 comments on commit 1b2b9c1

Please sign in to comment.