diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index 19bc24b8b..ea5125711 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -548,3 +548,61 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, ], }); + +new RuleTester({ + parser: require.resolve('@typescript-eslint/parser'), +}).run('prefer-importing-jest-globals: typescript edition', rule, { + valid: [], + invalid: [ + { + code: dedent` + import describe from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + import { describe, expect, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + const {describe} = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'script' }, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + ], +}); diff --git a/src/rules/prefer-importing-jest-globals.ts b/src/rules/prefer-importing-jest-globals.ts index 9cce2d213..49a5116bc 100644 --- a/src/rules/prefer-importing-jest-globals.ts +++ b/src/rules/prefer-importing-jest-globals.ts @@ -94,7 +94,9 @@ export default createRule({ return; } - const isModule = context.parserOptions.sourceType === 'module'; + const isModule = + context.parserOptions.sourceType === 'module' || + context.languageOptions?.sourceType === 'module'; context.report({ node: reportingNode,