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: use the last reference definition when checking jest fn scope #1109

Merged
merged 2 commits into from May 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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