Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v28.8.2
Choose a base ref
...
head repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v28.8.3
Choose a head ref
  • 2 commits
  • 4 files changed
  • 3 contributors

Commits on Sep 4, 2024

  1. fix(prefer-importing-jest-globals): don't add imports in the middle o…

    …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>
    ejzimmer and ezimmer-atlassian authored Sep 4, 2024
    Copy the full SHA
    9c4197c View commit details
  2. chore(release): 28.8.3 [skip ci]

    ## [28.8.3](v28.8.2...v28.8.3) (2024-09-04)
    
    ### Bug Fixes
    
    * **prefer-importing-jest-globals:** don't add imports in the middle of statements ([#1645](#1645)) ([9c4197c](9c4197c))
    semantic-release-bot committed Sep 4, 2024
    Copy the full SHA
    497a500 View commit details
Showing with 68 additions and 6 deletions.
  1. +7 −0 CHANGELOG.md
  2. +1 −1 package.json
  3. +59 −4 src/rules/__tests__/prefer-importing-jest-globals.test.ts
  4. +1 −1 src/rules/prefer-importing-jest-globals.ts
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [28.8.3](https://github.com/jest-community/eslint-plugin-jest/compare/v28.8.2...v28.8.3) (2024-09-04)


### Bug Fixes

* **prefer-importing-jest-globals:** don't add imports in the middle of statements ([#1645](https://github.com/jest-community/eslint-plugin-jest/issues/1645)) ([9c4197c](https://github.com/jest-community/eslint-plugin-jest/commit/9c4197c91fa96d7991acba8eac4fca909f28f8d0))

## [28.8.2](https://github.com/jest-community/eslint-plugin-jest/compare/v28.8.1...v28.8.2) (2024-09-02)


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest",
"version": "28.8.2",
"version": "28.8.3",
"description": "ESLint rules for Jest",
"keywords": [
"eslint",
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
@@ -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) => {});
});
@@ -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) => {});
});
@@ -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',
},
],
},
],
});

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

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