From 307f6a7b3aad7a5c891d8fea9f115e5d2f4f3fbb Mon Sep 17 00:00:00 2001 From: Erin Zimmer Date: Fri, 30 Aug 2024 06:01:44 +1000 Subject: [PATCH] fix(prefer-importing-jest-globals): support typescript-eslint parser (#1639) * fix(prefer-importing-jest-globals): support new config * fix(prefer-importing-jest-globals): tests, broken due to types * fix(prefer-importing-jest-globals): update tests * fix(prefer-importing-jest-globals): include valid tests * fix(prefer-importing-jest-globals): split typescript tests --------- Co-authored-by: Erin Zimmer --- .../prefer-importing-jest-globals.test.ts | 58 +++++++++++++++++++ src/rules/prefer-importing-jest-globals.ts | 4 +- 2 files changed, 61 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 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,