Skip to content

Commit 9c4197c

Browse files
fix(prefer-importing-jest-globals): don't add imports in the middle of statements (#1645)
* fix(prefer-importing-jest-globals): ensure imports aren't inserted in the middle of a statement * fix(prefer-importing-jest-globals): fix indenting * fix(prefer-importing-jest-globals): give commonjs and esmodules the same import behaviour --------- Co-authored-by: Erin Zimmer <[email protected]>
1 parent 9adda0a commit 9c4197c

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

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

+59-4
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
});
@@ -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
});
@@ -546,6 +546,61 @@ 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+
const { describe, expect, jest, test } = require('@jest/globals');
560+
console.log('hello');
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
});
551606

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default createRule({
152152

153153
if (requireNode?.type !== AST_NODE_TYPES.VariableDeclaration) {
154154
return fixer.insertTextBefore(
155-
reportingNode,
155+
firstNode,
156156
`${createFixerImports(isModule, functionsToImport)}\n`,
157157
);
158158
}

0 commit comments

Comments
 (0)