From b0e52d8887b03c68d67a3960e762152fea293a68 Mon Sep 17 00:00:00 2001 From: Erin Zimmer Date: Wed, 7 Aug 2024 08:14:29 +1000 Subject: [PATCH 1/5] fix(prefer-importing-jest-globals): support new config --- src/rules/prefer-importing-jest-globals.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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, From 367467c52a7b63f964f972dc30161eb128688ce5 Mon Sep 17 00:00:00 2001 From: Erin Zimmer Date: Thu, 8 Aug 2024 08:17:45 +1000 Subject: [PATCH 2/5] fix(prefer-importing-jest-globals): tests, broken due to types --- .../prefer-importing-jest-globals.test.ts | 735 +++++++++--------- 1 file changed, 384 insertions(+), 351 deletions(-) diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index 19bc24b8b..ab6549c5c 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -1,8 +1,12 @@ +import type { + InvalidTestCase, + ValidTestCase, +} from '@typescript-eslint/utils/ts-eslint'; import dedent from 'dedent'; import rule from '../prefer-importing-jest-globals'; import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils'; -const ruleTester = new RuleTester({ +const ruleTesterWithOldConfig = new RuleTester({ parser: espreeParser, parserOptions: { ecmaVersion: 2015, @@ -10,91 +14,124 @@ const ruleTester = new RuleTester({ }, }); -ruleTester.run('prefer-importing-jest-globals', rule, { - valid: [ - { - code: dedent` - // with import - import { test, expect } from '@jest/globals'; - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - options: [{ types: ['jest'] }], - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - const { it } = require('@jest/globals'); - it('should pass', () => { - expect(true).toBeDefined(); - }); - `, - options: [{ types: ['test'] }], - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - // with require - const { test, expect } = require('@jest/globals'); - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - }, - { - code: dedent` - const { test, expect } = require(\`@jest/globals\`); - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - }, - { - code: dedent` - import { it as itChecks } from '@jest/globals'; - itChecks("foo"); - `, - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - const { test } = require('@jest/globals'); - test("foo"); - `, - }, +const ruleTesterWithFlatConfig = new RuleTester({ + parser: espreeParser, + languageOptions: { + ecmaVersion: 2015, + sourceType: 'script', + }, +}); + +const valid: Array> = [ + { + code: dedent` + // with import + import { test, expect } from '@jest/globals'; + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + options: [{ types: ['jest'] }], + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + const { it } = require('@jest/globals'); + it('should pass', () => { + expect(true).toBeDefined(); + }); + `, + options: [{ types: ['test'] }], + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + // with require + const { test, expect } = require('@jest/globals'); + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + }, + { + code: dedent` + const { test, expect } = require(\`@jest/globals\`); + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + }, + { + code: dedent` + import { it as itChecks } from '@jest/globals'; + itChecks("foo"); + `, + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + const { test } = require('@jest/globals'); + test("foo"); + `, + }, + { + code: dedent` + const { test } = require('my-test-library'); + test("foo"); + `, + }, +]; + +const invalidScripts: Array> = + [ { code: dedent` - const { test } = require('my-test-library'); - test("foo"); - `, + 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(); + }) + `, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], }, - ], - invalid: [ { code: dedent` - import describe from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + const {describe} = require(\`@jest/globals\`); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + // todo: we should really maintain the template literals output: dedent` - import { describe, expect, test } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - parserOptions: { sourceType: 'module' }, + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 7, @@ -106,84 +143,75 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, { code: dedent` - jest.useFakeTimers(); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + const source = 'globals'; + const {describe} = require(\`@jest/\${source}\`); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + // todo: this shouldn't be indenting the "test" output: dedent` - import { jest } from '@jest/globals'; - jest.useFakeTimers(); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - options: [{ types: ['jest'] }], - parserOptions: { sourceType: 'module' }, + const source = 'globals'; + const {describe} = require(\`@jest/\${source}\`); + describe("suite", () => { + const { expect, test } = require('@jest/globals'); + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { - endColumn: 5, - column: 1, - line: 1, + endColumn: 7, + column: 3, + line: 4, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - import React from 'react'; - import { yourFunction } from './yourFile'; - import something from "something"; - import { test } from '@jest/globals'; - import { xit } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + const { [() => {}]: it } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - import React from 'react'; - import { yourFunction } from './yourFile'; - import something from "something"; - import { describe, expect, test } from '@jest/globals'; - import { xit } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - parserOptions: { sourceType: 'module' }, + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, column: 1, - line: 6, + line: 2, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - console.log('hello'); - import * as fs from 'fs'; - const { test, 'describe': describe } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + console.log('hello'); + const fs = require('fs'); + const { test, 'describe': describe } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - console.log('hello'); - import * as fs from 'fs'; - import { describe, expect, test } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - parserOptions: { sourceType: 'module' }, + console.log('hello'); + const fs = require('fs'); + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, @@ -195,22 +223,21 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, { code: dedent` - console.log('hello'); - import jestGlobals from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + console.log('hello'); + const jestGlobals = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - console.log('hello'); - import { describe, expect, jestGlobals, test } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - parserOptions: { sourceType: 'module' }, + console.log('hello'); + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, @@ -222,19 +249,18 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, { code: dedent` - import { pending } from 'actions'; - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, + const { pending } = require('actions'); + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, output: dedent` - import { pending } from 'actions'; - import { describe, test } from '@jest/globals'; - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, - parserOptions: { sourceType: 'module' }, + const { pending } = require('actions'); + const { describe, test } = require('@jest/globals'); + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, errors: [ { endColumn: 9, @@ -246,96 +272,69 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, { code: dedent` - const {describe} = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { - endColumn: 7, - column: 3, - line: 3, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - const {describe} = require(\`@jest/globals\`); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - // todo: we should really maintain the template literals - output: dedent` - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 7, - column: 3, - line: 3, + endColumn: 9, + column: 1, + line: 1, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - const source = 'globals'; - const {describe} = require(\`@jest/\${source}\`); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - // todo: this shouldn't be indenting the "test" + #!/usr/bin/env node + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - const source = 'globals'; - const {describe} = require(\`@jest/\${source}\`); - describe("suite", () => { - const { expect, test } = require('@jest/globals'); - test("foo"); - expect(true).toBeDefined(); - }) - `, + #!/usr/bin/env node + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'script' }, errors: [ { - endColumn: 7, - column: 3, - line: 4, + endColumn: 9, + column: 1, + line: 2, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - const { [() => {}]: it } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + // with comment above + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + // with comment above + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, @@ -347,98 +346,102 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, { code: dedent` - console.log('hello'); - const fs = require('fs'); - const { test, 'describe': describe } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + 'use strict'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - console.log('hello'); - const fs = require('fs'); - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + 'use strict'; + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, - column: 3, - line: 6, + column: 1, + line: 2, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - console.log('hello'); - const jestGlobals = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + \`use strict\`; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - console.log('hello'); - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + \`use strict\`; + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, column: 1, - line: 3, + line: 2, messageId: 'preferImportingJestGlobal', }, ], }, + ]; + +const invalidModules: Array> = + [ { code: dedent` - const { pending } = require('actions'); - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, + import describe from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - const { pending } = require('actions'); - const { describe, test } = require('@jest/globals'); - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, + import { describe, expect, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { - endColumn: 9, - column: 1, - line: 2, + endColumn: 7, + column: 3, + line: 3, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + jest.useFakeTimers(); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + import { jest } from '@jest/globals'; + jest.useFakeTimers(); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + options: [{ types: ['jest'] }], errors: [ { - endColumn: 9, + endColumn: 5, column: 1, line: 1, messageId: 'preferImportingJestGlobal', @@ -447,96 +450,106 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, { code: dedent` - #!/usr/bin/env node - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + import React from 'react'; + import { yourFunction } from './yourFile'; + import something from "something"; + import { test } from '@jest/globals'; + import { xit } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - #!/usr/bin/env node - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - parserOptions: { sourceType: 'script' }, + import React from 'react'; + import { yourFunction } from './yourFile'; + import something from "something"; + import { describe, expect, test } from '@jest/globals'; + import { xit } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, column: 1, - line: 2, + line: 6, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - // with comment above - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + console.log('hello'); + import * as fs from 'fs'; + const { test, 'describe': describe } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - // with comment above - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + console.log('hello'); + import * as fs from 'fs'; + import { describe, expect, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, errors: [ { endColumn: 9, - column: 1, - line: 2, + column: 3, + line: 6, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - 'use strict'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + console.log('hello'); + import jestGlobals from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - 'use strict'; - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + console.log('hello'); + import { describe, expect, jestGlobals, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, errors: [ { endColumn: 9, column: 1, - line: 2, + line: 3, messageId: 'preferImportingJestGlobal', }, ], }, { code: dedent` - \`use strict\`; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + import { pending } from 'actions'; + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, output: dedent` - \`use strict\`; - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + import { pending } from 'actions'; + import { describe, test } from '@jest/globals'; + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, + parserOptions: { sourceType: 'module' }, errors: [ { endColumn: 9, @@ -546,5 +559,25 @@ ruleTester.run('prefer-importing-jest-globals', rule, { }, ], }, + ]; + +ruleTesterWithOldConfig.run('prefer-importing-jest-globals', rule, { + valid, + invalid: [ + ...invalidScripts, + ...invalidModules.map(test => ({ + ...test, + parserOptions: { sourceType: 'module' }, + })), + ], +}); +ruleTesterWithFlatConfig.run('prefer-importing-jest-globals', rule, { + valid, + invalid: [ + ...invalidScripts, + ...invalidModules.map(test => ({ + ...test, + langugageOptions: { sourceType: 'module' }, + })), ], }); From 60812f48e5697e6545452f65ad70091527aaf0c2 Mon Sep 17 00:00:00 2001 From: Erin Zimmer Date: Thu, 22 Aug 2024 07:58:13 +1000 Subject: [PATCH 3/5] fix(prefer-importing-jest-globals): update tests --- .../prefer-importing-jest-globals.test.ts | 584 +----------------- 1 file changed, 31 insertions(+), 553 deletions(-) diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index ab6549c5c..cf8756004 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -1,137 +1,28 @@ -import type { - InvalidTestCase, - ValidTestCase, -} from '@typescript-eslint/utils/ts-eslint'; import dedent from 'dedent'; import rule from '../prefer-importing-jest-globals'; -import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils'; +import { FlatCompatRuleTester as RuleTester } from './test-utils'; -const ruleTesterWithOldConfig = new RuleTester({ - parser: espreeParser, - parserOptions: { - ecmaVersion: 2015, - sourceType: 'script', - }, -}); - -const ruleTesterWithFlatConfig = new RuleTester({ - parser: espreeParser, - languageOptions: { - ecmaVersion: 2015, - sourceType: 'script', - }, -}); - -const valid: Array> = [ - { - code: dedent` - // with import - import { test, expect } from '@jest/globals'; - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - options: [{ types: ['jest'] }], - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - const { it } = require('@jest/globals'); - it('should pass', () => { - expect(true).toBeDefined(); - }); - `, - options: [{ types: ['test'] }], - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - // with require - const { test, expect } = require('@jest/globals'); - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - }, - { - code: dedent` - const { test, expect } = require(\`@jest/globals\`); - test('should pass', () => { - expect(true).toBeDefined(); - }); - `, - }, - { - code: dedent` - import { it as itChecks } from '@jest/globals'; - itChecks("foo"); - `, - parserOptions: { sourceType: 'module' }, - }, - { - code: dedent` - const { test } = require('@jest/globals'); - test("foo"); - `, - }, - { - code: dedent` - const { test } = require('my-test-library'); - test("foo"); - `, - }, -]; - -const invalidScripts: Array> = - [ - { - 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(); - }) - `, - errors: [ - { - endColumn: 7, - column: 3, - line: 3, - messageId: 'preferImportingJestGlobal', - }, - ], - }, +new RuleTester({ + parser: require.resolve('@typescript-eslint/parser'), +}).run('prefer-importing-jest-globals: typescript edition', rule, { + valid: [], + invalid: [ { code: dedent` - const {describe} = require(\`@jest/globals\`); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - // todo: we should really maintain the template literals + import describe from '@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(); - }) - `, + import { describe, expect, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, errors: [ { endColumn: 7, @@ -143,276 +34,20 @@ const invalidScripts: Array> = }, { code: dedent` - const source = 'globals'; - const {describe} = require(\`@jest/\${source}\`); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - // todo: this shouldn't be indenting the "test" - output: dedent` - const source = 'globals'; - const {describe} = require(\`@jest/\${source}\`); - describe("suite", () => { - const { expect, test } = require('@jest/globals'); - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 7, - column: 3, - line: 4, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - const { [() => {}]: it } = 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(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - console.log('hello'); - const fs = require('fs'); - const { test, 'describe': describe } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - console.log('hello'); - const fs = require('fs'); - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 3, - line: 6, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - console.log('hello'); - const jestGlobals = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - console.log('hello'); - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 3, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - const { pending } = require('actions'); - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, - output: dedent` - const { pending } = require('actions'); - const { describe, test } = require('@jest/globals'); - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 1, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - #!/usr/bin/env node - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + const {describe} = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, output: dedent` - #!/usr/bin/env node - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, parserOptions: { sourceType: 'script' }, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - // with comment above - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - // with comment above - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - 'use strict'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - 'use strict'; - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - \`use strict\`; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - \`use strict\`; - const { describe, expect, test } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - ]; - -const invalidModules: Array> = - [ - { - 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(); - }) - `, errors: [ { endColumn: 7, @@ -422,162 +57,5 @@ const invalidModules: Array> = }, ], }, - { - code: dedent` - jest.useFakeTimers(); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - import { jest } from '@jest/globals'; - jest.useFakeTimers(); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - options: [{ types: ['jest'] }], - errors: [ - { - endColumn: 5, - column: 1, - line: 1, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - import React from 'react'; - import { yourFunction } from './yourFile'; - import something from "something"; - import { test } from '@jest/globals'; - import { xit } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - import React from 'react'; - import { yourFunction } from './yourFile'; - import something from "something"; - import { describe, expect, test } from '@jest/globals'; - import { xit } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 1, - line: 6, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - console.log('hello'); - import * as fs from 'fs'; - const { test, 'describe': describe } = require('@jest/globals'); - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - console.log('hello'); - import * as fs from 'fs'; - import { describe, expect, test } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - errors: [ - { - endColumn: 9, - column: 3, - line: 6, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - console.log('hello'); - import jestGlobals from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - output: dedent` - console.log('hello'); - import { describe, expect, jestGlobals, test } from '@jest/globals'; - describe("suite", () => { - test("foo"); - expect(true).toBeDefined(); - }) - `, - parserOptions: { sourceType: 'module' }, - errors: [ - { - endColumn: 9, - column: 1, - line: 3, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - { - code: dedent` - import { pending } from 'actions'; - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, - output: dedent` - import { pending } from 'actions'; - import { describe, test } from '@jest/globals'; - describe('foo', () => { - test.each(['hello', 'world'])("%s", (a) => {}); - }); - `, - parserOptions: { sourceType: 'module' }, - errors: [ - { - endColumn: 9, - column: 1, - line: 2, - messageId: 'preferImportingJestGlobal', - }, - ], - }, - ]; - -ruleTesterWithOldConfig.run('prefer-importing-jest-globals', rule, { - valid, - invalid: [ - ...invalidScripts, - ...invalidModules.map(test => ({ - ...test, - parserOptions: { sourceType: 'module' }, - })), - ], -}); -ruleTesterWithFlatConfig.run('prefer-importing-jest-globals', rule, { - valid, - invalid: [ - ...invalidScripts, - ...invalidModules.map(test => ({ - ...test, - langugageOptions: { sourceType: 'module' }, - })), ], }); From 4f6cf0a8e166c835565a893874bb50981042dd81 Mon Sep 17 00:00:00 2001 From: Erin Zimmer Date: Thu, 22 Aug 2024 12:05:30 +1000 Subject: [PATCH 4/5] fix(prefer-importing-jest-globals): include valid tests --- .../prefer-importing-jest-globals.test.ts | 68 ++++++++++++++++++- 1 file changed, 67 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 cf8756004..0f16a1a84 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -5,7 +5,73 @@ import { FlatCompatRuleTester as RuleTester } from './test-utils'; new RuleTester({ parser: require.resolve('@typescript-eslint/parser'), }).run('prefer-importing-jest-globals: typescript edition', rule, { - valid: [], + valid: [ + { + code: dedent` + // with import + import { test, expect } from '@jest/globals'; + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + options: [{ types: ['jest'] }], + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + const { it } = require('@jest/globals'); + it('should pass', () => { + expect(true).toBeDefined(); + }); + `, + options: [{ types: ['test'] }], + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + // with require + const { test, expect } = require('@jest/globals'); + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + }, + { + code: dedent` + const { test, expect } = require(\`@jest/globals\`); + test('should pass', () => { + expect(true).toBeDefined(); + }); + `, + }, + { + code: dedent` + import { it as itChecks } from '@jest/globals'; + itChecks("foo"); + `, + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + const { test } = require('@jest/globals'); + test("foo"); + `, + }, + { + code: dedent` + const { test } = require('my-test-library'); + test("foo"); + `, + }, + ], invalid: [ { code: dedent` From b78b2fda3854c5a8158ae3ae6a676f5bf6b1754d Mon Sep 17 00:00:00 2001 From: Erin Zimmer Date: Tue, 27 Aug 2024 07:58:13 +1000 Subject: [PATCH 5/5] fix(prefer-importing-jest-globals): split typescript tests --- .../prefer-importing-jest-globals.test.ts | 489 +++++++++++++++++- 1 file changed, 485 insertions(+), 4 deletions(-) diff --git a/src/rules/__tests__/prefer-importing-jest-globals.test.ts b/src/rules/__tests__/prefer-importing-jest-globals.test.ts index 0f16a1a84..ea5125711 100644 --- a/src/rules/__tests__/prefer-importing-jest-globals.test.ts +++ b/src/rules/__tests__/prefer-importing-jest-globals.test.ts @@ -1,10 +1,16 @@ import dedent from 'dedent'; import rule from '../prefer-importing-jest-globals'; -import { FlatCompatRuleTester as RuleTester } from './test-utils'; +import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils'; -new RuleTester({ - parser: require.resolve('@typescript-eslint/parser'), -}).run('prefer-importing-jest-globals: typescript edition', rule, { +const ruleTester = new RuleTester({ + parser: espreeParser, + parserOptions: { + ecmaVersion: 2015, + sourceType: 'script', + }, +}); + +ruleTester.run('prefer-importing-jest-globals', rule, { valid: [ { code: dedent` @@ -72,6 +78,481 @@ new RuleTester({ `, }, ], + 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` + jest.useFakeTimers(); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + import { jest } from '@jest/globals'; + jest.useFakeTimers(); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + options: [{ types: ['jest'] }], + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 5, + column: 1, + line: 1, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + import React from 'react'; + import { yourFunction } from './yourFile'; + import something from "something"; + import { test } from '@jest/globals'; + import { xit } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + import React from 'react'; + import { yourFunction } from './yourFile'; + import something from "something"; + import { describe, expect, test } from '@jest/globals'; + import { xit } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 9, + column: 1, + line: 6, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + console.log('hello'); + import * as fs from 'fs'; + const { test, 'describe': describe } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + console.log('hello'); + import * as fs from 'fs'; + import { describe, expect, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 9, + column: 3, + line: 6, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + console.log('hello'); + import jestGlobals from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + console.log('hello'); + import { describe, expect, jestGlobals, test } from '@jest/globals'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 9, + column: 1, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + import { pending } from 'actions'; + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, + output: dedent` + import { pending } from 'actions'; + import { describe, test } from '@jest/globals'; + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, + parserOptions: { sourceType: 'module' }, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + 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(); + }) + `, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + const {describe} = require(\`@jest/globals\`); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + // todo: we should really maintain the template literals + output: dedent` + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 7, + column: 3, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + const source = 'globals'; + const {describe} = require(\`@jest/\${source}\`); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + // todo: this shouldn't be indenting the "test" + output: dedent` + const source = 'globals'; + const {describe} = require(\`@jest/\${source}\`); + describe("suite", () => { + const { expect, test } = require('@jest/globals'); + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 7, + column: 3, + line: 4, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + const { [() => {}]: it } = 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(); + }) + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + console.log('hello'); + const fs = require('fs'); + const { test, 'describe': describe } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + console.log('hello'); + const fs = require('fs'); + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 9, + column: 3, + line: 6, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + console.log('hello'); + const jestGlobals = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + console.log('hello'); + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 3, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + const { pending } = require('actions'); + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, + output: dedent` + const { pending } = require('actions'); + const { describe, test } = require('@jest/globals'); + describe('foo', () => { + test.each(['hello', 'world'])("%s", (a) => {}); + }); + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 1, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + #!/usr/bin/env node + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + #!/usr/bin/env node + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + parserOptions: { sourceType: 'script' }, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + // with comment above + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + // with comment above + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + 'use strict'; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + 'use strict'; + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + { + code: dedent` + \`use strict\`; + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + output: dedent` + \`use strict\`; + const { describe, expect, test } = require('@jest/globals'); + describe("suite", () => { + test("foo"); + expect(true).toBeDefined(); + }) + `, + errors: [ + { + endColumn: 9, + column: 1, + line: 2, + messageId: 'preferImportingJestGlobal', + }, + ], + }, + ], +}); + +new RuleTester({ + parser: require.resolve('@typescript-eslint/parser'), +}).run('prefer-importing-jest-globals: typescript edition', rule, { + valid: [], invalid: [ { code: dedent`