Skip to content

Commit f33e4d9

Browse files
fix(prefer-importing-jest-globals): give commonjs and esmodules the same import behaviour
1 parent 0f41ddc commit f33e4d9

File tree

2 files changed

+6
-26
lines changed

2 files changed

+6
-26
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,11 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
304304
`,
305305
// todo: this shouldn't be indenting the "test"
306306
output: dedent`
307+
const { expect, test } = require('@jest/globals');
307308
const source = 'globals';
308309
const {describe} = require(\`@jest/\${source}\`);
309310
describe("suite", () => {
310-
const { expect, test } = require('@jest/globals');
311-
test("foo");
311+
test("foo");
312312
expect(true).toBeDefined();
313313
})
314314
`,
@@ -407,8 +407,8 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
407407
});
408408
`,
409409
output: dedent`
410-
const { pending } = require('actions');
411410
const { describe, test } = require('@jest/globals');
411+
const { pending } = require('actions');
412412
describe('foo', () => {
413413
test.each(['hello', 'world'])("%s", (a) => {});
414414
});
@@ -552,12 +552,12 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
552552
const onClick = jest.fn();
553553
describe("suite", () => {
554554
test("foo");
555-
expect(onClick).toHaveBeenCalled();
555+
expect(onClick).toHaveBeenCalled();
556556
})
557557
`,
558558
output: dedent`
559-
console.log('hello');
560559
const { describe, expect, jest, test } = require('@jest/globals');
560+
console.log('hello');
561561
const onClick = jest.fn();
562562
describe("suite", () => {
563563
test("foo");

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ 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-
4024
const allJestFnTypes: JestFnType[] = [
4125
'hook',
4226
'describe',
@@ -167,12 +151,8 @@ export default createRule({
167151
);
168152

169153
if (requireNode?.type !== AST_NODE_TYPES.VariableDeclaration) {
170-
const insertBeforeNode = isModule
171-
? firstNode
172-
: findInsertionPoint(reportingNode);
173-
174154
return fixer.insertTextBefore(
175-
insertBeforeNode,
155+
firstNode,
176156
`${createFixerImports(isModule, functionsToImport)}\n`,
177157
);
178158
}

0 commit comments

Comments
 (0)