From 86d4b77e772c268fac15584305075b3fcbf2cf18 Mon Sep 17 00:00:00 2001 From: Gareth Jones <3151613+G-Rath@users.noreply.github.com> Date: Thu, 12 Jun 2025 08:17:44 +1200 Subject: [PATCH] test: automatically skip ecma 2022 tests when using ESLint v7 --- src/rules/__tests__/test-utils.ts | 42 ++++- .../utils/__tests__/parseJestFnCall.test.ts | 147 +++++++++--------- 2 files changed, 112 insertions(+), 77 deletions(-) diff --git a/src/rules/__tests__/test-utils.ts b/src/rules/__tests__/test-utils.ts index ac27f8318..e4fe80132 100644 --- a/src/rules/__tests__/test-utils.ts +++ b/src/rules/__tests__/test-utils.ts @@ -25,11 +25,49 @@ export class FlatCompatRuleTester extends TSESLint.RuleTester { tests: TSESLint.RunTests, ) { super.run(ruleName, rule, { - valid: tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)), - invalid: tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)), + valid: FlatCompatRuleTester._filterCases( + tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)), + ), + invalid: FlatCompatRuleTester._filterCases( + tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)), + ), }); } + /* istanbul ignore next */ + /** + * Filters out test cases that are using ecma version 2022 or higher when running + * on ESLint v7 + * @private + */ + private static _filterCases< + T extends + | string + | TSESLint.ValidTestCase + | TSESLint.InvalidTestCase, + >(tests: T[]): T[] { + if (semver.major(eslintVersion) > 7) { + return tests; + } + + const filtered = tests.filter( + t => + typeof t === 'string' || + !t.parserOptions?.ecmaVersion || + t.parserOptions.ecmaVersion === 'latest' || + t.parserOptions.ecmaVersion < 2022, + ); + + // print the number of tests that were filtered + if (filtered.length !== tests.length) { + console.warn( + `Filtered ${tests.length - filtered.length} tests due to unsupported parser options.`, + ); + } + + return filtered; + } + /* istanbul ignore next */ private static _flatCompat< T extends diff --git a/src/rules/utils/__tests__/parseJestFnCall.test.ts b/src/rules/utils/__tests__/parseJestFnCall.test.ts index da197c9f1..1b445ea09 100644 --- a/src/rules/utils/__tests__/parseJestFnCall.test.ts +++ b/src/rules/utils/__tests__/parseJestFnCall.test.ts @@ -2,7 +2,6 @@ import type { TSESTree } from '@typescript-eslint/utils'; import dedent from 'dedent'; import { FlatCompatRuleTester as RuleTester, - eslintMajorVersion, espreeParser, } from '../../__tests__/test-utils'; import { @@ -445,82 +444,80 @@ ruleTester.run('esm', rule, { invalid: [], }); -if (eslintMajorVersion >= 8) { - ruleTester.run('esm (dynamic)', rule, { - valid: [ - { - code: dedent` - const { it } = await import('./test-utils'); +ruleTester.run('esm (dynamic)', rule, { + valid: [ + { + code: dedent` + const { it } = await import('./test-utils'); - it('is not a jest function', () => {}); - `, - parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, - }, - { - code: dedent` - const { it } = await import(\`./test-utils\`); + it('is not a jest function', () => {}); + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + }, + { + code: dedent` + const { it } = await import(\`./test-utils\`); - it('is not a jest function', () => {}); - `, - parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, - }, - ], - invalid: [ - { - code: dedent` - const { it } = await import("@jest/globals"); - - it('is a jest function', () => {}); - `, - parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, - errors: [ - { - messageId: 'details', - data: expectedParsedJestFnCallResultData({ - name: 'it', - type: 'test', - head: { - original: 'it', - local: 'it', - type: 'import', - node: 'it', - }, - members: [], - }), - column: 1, - line: 3, - }, - ], - }, - { - code: dedent` - const { it } = await import(\`@jest/globals\`); - - it('is a jest function', () => {}); - `, - parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, - errors: [ - { - messageId: 'details', - data: expectedParsedJestFnCallResultData({ - name: 'it', - type: 'test', - head: { - original: 'it', - local: 'it', - type: 'import', - node: 'it', - }, - members: [], - }), - column: 1, - line: 3, - }, - ], - }, - ], - }); -} + it('is not a jest function', () => {}); + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + }, + ], + invalid: [ + { + code: dedent` + const { it } = await import("@jest/globals"); + + it('is a jest function', () => {}); + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + errors: [ + { + messageId: 'details', + data: expectedParsedJestFnCallResultData({ + name: 'it', + type: 'test', + head: { + original: 'it', + local: 'it', + type: 'import', + node: 'it', + }, + members: [], + }), + column: 1, + line: 3, + }, + ], + }, + { + code: dedent` + const { it } = await import(\`@jest/globals\`); + + it('is a jest function', () => {}); + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2022 }, + errors: [ + { + messageId: 'details', + data: expectedParsedJestFnCallResultData({ + name: 'it', + type: 'test', + head: { + original: 'it', + local: 'it', + type: 'import', + node: 'it', + }, + members: [], + }), + column: 1, + line: 3, + }, + ], + }, + ], +}); ruleTester.run('cjs', rule, { valid: [