diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index acd6a1b5c..fde6afd48 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -304,11 +304,11 @@ ruleTester.run('prefer-importing-jest-globals', rule, { `, // todo: this shouldn't be indenting the "test" output: dedent` + const { expect, test } = require('@jest/globals'); const source = 'globals'; const {describe} = require(\`@jest/\${source}\`); describe("suite", () => { - const { expect, test } = require('@jest/globals'); - test("foo"); + test("foo"); expect(true).toBeDefined(); }) `, @@ -407,8 +407,8 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }); `, output: dedent` - const { pending } = require('actions'); const { describe, test } = require('@jest/globals'); + const { pending } = require('actions'); describe('foo', () => { test.each(['hello', 'world'])("%s", (a) => {}); }); @@ -552,12 +552,12 @@ ruleTester.run('prefer-importing-jest-globals', rule, { const onClick = jest.fn(); describe("suite", () => { test("foo"); - expect(onClick).toHaveBeenCalled(); + expect(onClick).toHaveBeenCalled(); }) `, output: dedent` - console.log('hello'); const { describe, expect, jest, test } = require('@jest/globals'); + console.log('hello'); const onClick = jest.fn(); describe("suite", () => { test("foo"); diff --git a/src/rules/prefer-importing-jest-globals.ts b/src/rules/prefer-importing-jest-globals.ts index f84c64e09..66afbd3f0 100644 --- a/src/rules/prefer-importing-jest-globals.ts +++ b/src/rules/prefer-importing-jest-globals.ts @@ -21,22 +21,6 @@ const createFixerImports = ( : `const { ${allImportsFormatted} } = require('@jest/globals');`; }; -const findInsertionPoint = (reportingNode: TSESTree.Node) => { - let currentNode = reportingNode; - - while ( - currentNode.parent && - currentNode.parent.type !== AST_NODE_TYPES.Program && - currentNode.parent.type !== AST_NODE_TYPES.VariableDeclaration - ) { - currentNode = currentNode.parent; - } - - return currentNode.parent?.type === AST_NODE_TYPES.VariableDeclaration - ? currentNode.parent - : reportingNode; -}; - const allJestFnTypes: JestFnType[] = [ 'hook', 'describe', @@ -167,12 +151,8 @@ export default createRule({ ); if (requireNode?.type !== AST_NODE_TYPES.VariableDeclaration) { - const insertBeforeNode = isModule - ? firstNode - : findInsertionPoint(reportingNode); - return fixer.insertTextBefore( - insertBeforeNode, + firstNode, `${createFixerImports(isModule, functionsToImport)}\n`, ); }