Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/rules/__tests__/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,49 @@ export class FlatCompatRuleTester extends TSESLint.RuleTester {
tests: TSESLint.RunTests<TMessageIds, TOptions>,
) {
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<unknown[]>
| TSESLint.InvalidTestCase<string, unknown[]>,
>(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
Expand Down
147 changes: 72 additions & 75 deletions src/rules/utils/__tests__/parseJestFnCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: [
Expand Down