Skip to content

Commit

Permalink
fix(prefer-importing-jest-globals): don't add imports in the middle o…
Browse files Browse the repository at this point in the history
…f 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 <ezimmer@atlassian.com>
  • Loading branch information
ejzimmer and ezimmer-atlassian authored Sep 4, 2024
1 parent 9adda0a commit 9c4197c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
63 changes: 59 additions & 4 deletions src/rules/__tests__/prefer-importing-jest-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
});
`,
output: dedent`
import { pending } from 'actions';
import { describe, test } from '@jest/globals';
import { pending } from 'actions';
describe('foo', () => {
test.each(['hello', 'world'])("%s", (a) => {});
});
Expand Down Expand Up @@ -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();
})
`,
Expand Down Expand Up @@ -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) => {});
});
Expand Down Expand Up @@ -546,6 +546,61 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
},
],
},
{
code: dedent`
console.log('hello');
const onClick = jest.fn();
describe("suite", () => {
test("foo");
expect(onClick).toHaveBeenCalled();
})
`,
output: dedent`
const { describe, expect, jest, test } = require('@jest/globals');
console.log('hello');
const onClick = jest.fn();
describe("suite", () => {
test("foo");
expect(onClick).toHaveBeenCalled();
})
`,
errors: [
{
endColumn: 21,
column: 17,
line: 2,
messageId: 'preferImportingJestGlobal',
},
],
},
{
code: dedent`
console.log('hello');
const onClick = jest.fn();
describe("suite", () => {
test("foo");
expect(onClick).toHaveBeenCalled();
})
`,
output: dedent`
import { describe, expect, jest, test } from '@jest/globals';
console.log('hello');
const onClick = jest.fn();
describe("suite", () => {
test("foo");
expect(onClick).toHaveBeenCalled();
})
`,
parserOptions: { sourceType: 'module' },
errors: [
{
endColumn: 21,
column: 17,
line: 2,
messageId: 'preferImportingJestGlobal',
},
],
},
],
});

Expand Down
2 changes: 1 addition & 1 deletion src/rules/prefer-importing-jest-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default createRule({

if (requireNode?.type !== AST_NODE_TYPES.VariableDeclaration) {
return fixer.insertTextBefore(
reportingNode,
firstNode,
`${createFixerImports(isModule, functionsToImport)}\n`,
);
}
Expand Down

0 comments on commit 9c4197c

Please # to comment.