Skip to content

Commit 078df22

Browse files
authored
test: automatically skip ecma 2022 tests when using ESLint v7 (#1759)
1 parent ab990dd commit 078df22

File tree

2 files changed

+112
-77
lines changed

2 files changed

+112
-77
lines changed

src/rules/__tests__/test-utils.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,49 @@ export class FlatCompatRuleTester extends TSESLint.RuleTester {
2525
tests: TSESLint.RunTests<TMessageIds, TOptions>,
2626
) {
2727
super.run(ruleName, rule, {
28-
valid: tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)),
29-
invalid: tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)),
28+
valid: FlatCompatRuleTester._filterCases(
29+
tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)),
30+
),
31+
invalid: FlatCompatRuleTester._filterCases(
32+
tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)),
33+
),
3034
});
3135
}
3236

37+
/* istanbul ignore next */
38+
/**
39+
* Filters out test cases that are using ecma version 2022 or higher when running
40+
* on ESLint v7
41+
* @private
42+
*/
43+
private static _filterCases<
44+
T extends
45+
| string
46+
| TSESLint.ValidTestCase<unknown[]>
47+
| TSESLint.InvalidTestCase<string, unknown[]>,
48+
>(tests: T[]): T[] {
49+
if (semver.major(eslintVersion) > 7) {
50+
return tests;
51+
}
52+
53+
const filtered = tests.filter(
54+
t =>
55+
typeof t === 'string' ||
56+
!t.parserOptions?.ecmaVersion ||
57+
t.parserOptions.ecmaVersion === 'latest' ||
58+
t.parserOptions.ecmaVersion < 2022,
59+
);
60+
61+
// print the number of tests that were filtered
62+
if (filtered.length !== tests.length) {
63+
console.warn(
64+
`Filtered ${tests.length - filtered.length} tests due to unsupported parser options.`,
65+
);
66+
}
67+
68+
return filtered;
69+
}
70+
3371
/* istanbul ignore next */
3472
private static _flatCompat<
3573
T extends

src/rules/utils/__tests__/parseJestFnCall.test.ts

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { TSESTree } from '@typescript-eslint/utils';
22
import dedent from 'dedent';
33
import {
44
FlatCompatRuleTester as RuleTester,
5-
eslintMajorVersion,
65
espreeParser,
76
} from '../../__tests__/test-utils';
87
import {
@@ -445,82 +444,80 @@ ruleTester.run('esm', rule, {
445444
invalid: [],
446445
});
447446

448-
if (eslintMajorVersion >= 8) {
449-
ruleTester.run('esm (dynamic)', rule, {
450-
valid: [
451-
{
452-
code: dedent`
453-
const { it } = await import('./test-utils');
447+
ruleTester.run('esm (dynamic)', rule, {
448+
valid: [
449+
{
450+
code: dedent`
451+
const { it } = await import('./test-utils');
454452
455-
it('is not a jest function', () => {});
456-
`,
457-
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
458-
},
459-
{
460-
code: dedent`
461-
const { it } = await import(\`./test-utils\`);
453+
it('is not a jest function', () => {});
454+
`,
455+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
456+
},
457+
{
458+
code: dedent`
459+
const { it } = await import(\`./test-utils\`);
462460
463-
it('is not a jest function', () => {});
464-
`,
465-
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
466-
},
467-
],
468-
invalid: [
469-
{
470-
code: dedent`
471-
const { it } = await import("@jest/globals");
472-
473-
it('is a jest function', () => {});
474-
`,
475-
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
476-
errors: [
477-
{
478-
messageId: 'details',
479-
data: expectedParsedJestFnCallResultData({
480-
name: 'it',
481-
type: 'test',
482-
head: {
483-
original: 'it',
484-
local: 'it',
485-
type: 'import',
486-
node: 'it',
487-
},
488-
members: [],
489-
}),
490-
column: 1,
491-
line: 3,
492-
},
493-
],
494-
},
495-
{
496-
code: dedent`
497-
const { it } = await import(\`@jest/globals\`);
498-
499-
it('is a jest function', () => {});
500-
`,
501-
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
502-
errors: [
503-
{
504-
messageId: 'details',
505-
data: expectedParsedJestFnCallResultData({
506-
name: 'it',
507-
type: 'test',
508-
head: {
509-
original: 'it',
510-
local: 'it',
511-
type: 'import',
512-
node: 'it',
513-
},
514-
members: [],
515-
}),
516-
column: 1,
517-
line: 3,
518-
},
519-
],
520-
},
521-
],
522-
});
523-
}
461+
it('is not a jest function', () => {});
462+
`,
463+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
464+
},
465+
],
466+
invalid: [
467+
{
468+
code: dedent`
469+
const { it } = await import("@jest/globals");
470+
471+
it('is a jest function', () => {});
472+
`,
473+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
474+
errors: [
475+
{
476+
messageId: 'details',
477+
data: expectedParsedJestFnCallResultData({
478+
name: 'it',
479+
type: 'test',
480+
head: {
481+
original: 'it',
482+
local: 'it',
483+
type: 'import',
484+
node: 'it',
485+
},
486+
members: [],
487+
}),
488+
column: 1,
489+
line: 3,
490+
},
491+
],
492+
},
493+
{
494+
code: dedent`
495+
const { it } = await import(\`@jest/globals\`);
496+
497+
it('is a jest function', () => {});
498+
`,
499+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
500+
errors: [
501+
{
502+
messageId: 'details',
503+
data: expectedParsedJestFnCallResultData({
504+
name: 'it',
505+
type: 'test',
506+
head: {
507+
original: 'it',
508+
local: 'it',
509+
type: 'import',
510+
node: 'it',
511+
},
512+
members: [],
513+
}),
514+
column: 1,
515+
line: 3,
516+
},
517+
],
518+
},
519+
],
520+
});
524521

525522
ruleTester.run('cjs', rule, {
526523
valid: [

0 commit comments

Comments
 (0)