From 3a5af78febd4a0f71ac4297530d11fa9a0e785fb Mon Sep 17 00:00:00 2001 From: Gareth Jones <3151613+G-Rath@users.noreply.github.com> Date: Wed, 11 Jun 2025 08:12:16 +1200 Subject: [PATCH 1/2] fix(prefer-importing-jest-globals): preserve `as` imports (#1753) --- .../prefer-importing-jest-globals.test.ts | 54 +++++++++++++++++++ src/rules/prefer-importing-jest-globals.ts | 9 +++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index 33b331793..e71ead351 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -104,6 +104,60 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, ], }, + { + code: dedent` + import { describe as context } from '@jest/globals'; + context("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + import { describe as context, expect, test } from '@jest/globals'; + context("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + import { describe as context } from '@jest/globals'; + describe("something", () => { + context("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + }) + `, + output: dedent` + import { describe, describe as context, expect, test } from '@jest/globals'; + describe("something", () => { + context("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + }) + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, { code: dedent` jest.useFakeTimers(); diff --git a/src/rules/prefer-importing-jest-globals.ts b/src/rules/prefer-importing-jest-globals.ts index 66afbd3f0..612640ede 100644 --- a/src/rules/prefer-importing-jest-globals.ts +++ b/src/rules/prefer-importing-jest-globals.ts @@ -120,7 +120,14 @@ export default createRule({ specifier.type === AST_NODE_TYPES.ImportSpecifier && specifier.imported?.name ) { - functionsToImport.add(specifier.imported.name); + let importName = specifier.imported.name; + const local = getAccessorValue(specifier.local); + + if (local !== importName) { + importName = `${importName} as ${local}`; + } + + functionsToImport.add(importName); } if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier) { From 0c8e6d76506f2953d48efb690e438dcf9f50be40 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 10 Jun 2025 20:15:39 +0000 Subject: [PATCH 2/2] chore(release): 28.13.2 [skip ci] ## [28.13.2](https://github.com/jest-community/eslint-plugin-jest/compare/v28.13.1...v28.13.2) (2025-06-10) ### Bug Fixes * **prefer-importing-jest-globals:** preserve `as` imports ([#1753](https://github.com/jest-community/eslint-plugin-jest/issues/1753)) ([3a5af78](https://github.com/jest-community/eslint-plugin-jest/commit/3a5af78febd4a0f71ac4297530d11fa9a0e785fb)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b1511499..a0e2e9679 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [28.13.2](https://github.com/jest-community/eslint-plugin-jest/compare/v28.13.1...v28.13.2) (2025-06-10) + + +### Bug Fixes + +* **prefer-importing-jest-globals:** preserve `as` imports ([#1753](https://github.com/jest-community/eslint-plugin-jest/issues/1753)) ([3a5af78](https://github.com/jest-community/eslint-plugin-jest/commit/3a5af78febd4a0f71ac4297530d11fa9a0e785fb)) + ## [28.13.1](https://github.com/jest-community/eslint-plugin-jest/compare/v28.13.0...v28.13.1) (2025-06-10) diff --git a/package.json b/package.json index 26593de43..3998692d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-jest", - "version": "28.13.1", + "version": "28.13.2", "description": "ESLint rules for Jest", "keywords": [ "eslint",