Skip to content

Commit 090fd91

Browse files
fix(prefer-importing-jest-globals): ensure imports aren't inserted in the middle of a statement
1 parent 11ef4fc commit 090fd91

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/rules/__tests__/prefer-importing-jest-globals.test.ts

+56-1
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
228228
});
229229
`,
230230
output: dedent`
231-
import { pending } from 'actions';
232231
import { describe, test } from '@jest/globals';
232+
import { pending } from 'actions';
233233
describe('foo', () => {
234234
test.each(['hello', 'world'])("%s", (a) => {});
235235
});
@@ -546,5 +546,60 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
546546
},
547547
],
548548
},
549+
{
550+
code: dedent`
551+
console.log('hello');
552+
const onClick = jest.fn();
553+
describe("suite", () => {
554+
test("foo");
555+
expect(onClick).toHaveBeenCalled();
556+
})
557+
`,
558+
output: dedent`
559+
console.log('hello');
560+
const { describe, expect, jest, test } = require('@jest/globals');
561+
const onClick = jest.fn();
562+
describe("suite", () => {
563+
test("foo");
564+
expect(onClick).toHaveBeenCalled();
565+
})
566+
`,
567+
errors: [
568+
{
569+
endColumn: 21,
570+
column: 17,
571+
line: 2,
572+
messageId: 'preferImportingJestGlobal',
573+
},
574+
],
575+
},
576+
{
577+
code: dedent`
578+
console.log('hello');
579+
const onClick = jest.fn();
580+
describe("suite", () => {
581+
test("foo");
582+
expect(onClick).toHaveBeenCalled();
583+
})
584+
`,
585+
output: dedent`
586+
import { describe, expect, jest, test } from '@jest/globals';
587+
console.log('hello');
588+
const onClick = jest.fn();
589+
describe("suite", () => {
590+
test("foo");
591+
expect(onClick).toHaveBeenCalled();
592+
})
593+
`,
594+
parserOptions: { sourceType: 'module' },
595+
errors: [
596+
{
597+
endColumn: 21,
598+
column: 17,
599+
line: 2,
600+
messageId: 'preferImportingJestGlobal',
601+
},
602+
],
603+
},
549604
],
550605
});

src/rules/prefer-importing-jest-globals.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ const createFixerImports = (
2121
: `const { ${allImportsFormatted} } = require('@jest/globals');`;
2222
};
2323

24+
const findInsertionPoint = (reportingNode: TSESTree.Node) => {
25+
let currentNode = reportingNode;
26+
27+
while (
28+
currentNode.parent &&
29+
currentNode.parent.type !== AST_NODE_TYPES.Program &&
30+
currentNode.parent.type !== AST_NODE_TYPES.VariableDeclaration
31+
) {
32+
currentNode = currentNode.parent;
33+
}
34+
35+
return currentNode.parent?.type === AST_NODE_TYPES.VariableDeclaration
36+
? currentNode.parent
37+
: reportingNode;
38+
};
39+
2440
const allJestFnTypes: JestFnType[] = [
2541
'hook',
2642
'describe',
@@ -158,8 +174,12 @@ export default createRule({
158174
);
159175

160176
if (requireNode?.type !== AST_NODE_TYPES.VariableDeclaration) {
177+
const insertBeforeNode = isModule
178+
? firstNode
179+
: findInsertionPoint(reportingNode);
180+
161181
return fixer.insertTextBefore(
162-
reportingNode,
182+
insertBeforeNode,
163183
`${createFixerImports(isModule, functionsToImport)}\n`,
164184
);
165185
}

0 commit comments

Comments
 (0)