diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index 6331112ab..1040067ef 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -22,6 +22,16 @@ ruleTester.run('prefer-importing-jest-globals', rule, { `, parserOptions: { sourceType: 'module' }, }, + { + code: dedent` + // with import + import { 'test' as test, expect } from '@jest/globals'; + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + }, { code: dedent` test('should pass', () => { @@ -65,6 +75,13 @@ ruleTester.run('prefer-importing-jest-globals', rule, { `, parserOptions: { sourceType: 'module' }, }, + { + code: dedent` + import { 'it' as itChecks } from '@jest/globals'; + itChecks("foo"); + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + }, { code: dedent` const { test } = require('@jest/globals'); @@ -158,6 +175,56 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, ], }, + { + code: dedent` + import { 'describe' as describe } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + import { 'describe' as describe, expect, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + 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', ecmaVersion: 2022 }, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + 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 3ae5ba359..639185ee6 100644 --- a/src/rules/prefer-importing-jest-globals.ts +++ b/src/rules/prefer-importing-jest-globals.ts @@ -116,17 +116,18 @@ export default createRule({ if (importNode?.type === AST_NODE_TYPES.ImportDeclaration) { for (const specifier of importNode.specifiers) { - if ( - specifier.type === AST_NODE_TYPES.ImportSpecifier && - specifier.imported?.name - ) { - let importName = specifier.imported.name; + if (specifier.type === AST_NODE_TYPES.ImportSpecifier) { + let importName = specifier.imported.name ?? ''; const local = getAccessorValue(specifier.local); if (local !== importName) { importName = `${importName} as ${local}`; } + if ('value' in specifier.imported) { + importName = `'${specifier.imported.value}'${importName}`; + } + functionsToImport.add(importName); }