Skip to content

Commit fa5e416

Browse files
captaindonaldljharb
authored andcommitted
[Tests] strip 'type:' from RuleTester errors on eslint >= 10 via helper
Wrap invalid test cases in `tests/helpers/ruleTester.js` to strip `type` when the running ESLint is >= 10. On older versions the function does nothing, keeping all `type:` assertions intact.
1 parent 77c5fa8 commit fa5e416

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

tests/helpers/parsers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ const parsers = {
150150
|| features.has('jsx namespace')
151151
|| features.has('bind operator')
152152
|| features.has('do expressions');
153-
const tsOld = !skipTS && !features.has('no-ts-old');
153+
// typescript-eslint-parser (deprecated) cannot parse a TS 5 tsconfig, used by the eslint 10 matrix.
154+
const tsOld = !skipTS && !features.has('no-ts-old') && !semver.satisfies(version, '>= 10');
154155
const tsNew = !skipTS && !features.has('no-ts-new');
155156

156157
return [].concat(

tests/helpers/ruleTester.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ function convertToFlat(item, plugins) {
4747
return newItem;
4848
}
4949

50+
const eslintMajor = semver.major(eslintPkg.version);
51+
52+
function stripTypeOnEslint10(test) {
53+
if (eslintMajor < 10 || !test || typeof test !== 'object' || !Array.isArray(test.errors)) {
54+
return test;
55+
}
56+
return Object.assign({}, test, {
57+
errors: test.errors.map((err) => {
58+
if (!err || typeof err !== 'object' || !('type' in err)) {
59+
return err;
60+
}
61+
const next = Object.assign({}, err);
62+
delete next.type;
63+
return next;
64+
}),
65+
});
66+
}
67+
5068
let RuleTester = ESLintRuleTester;
5169

5270
if (semver.major(eslintPkg.version) >= 9) {
@@ -98,7 +116,9 @@ if (semver.major(eslintPkg.version) >= 9) {
98116
run(ruleName, rule, tests) {
99117
const newTests = {
100118
valid: tests.valid.map((test) => convertToFlat(test, this[PLUGINS])),
101-
invalid: tests.invalid.map((test) => convertToFlat(test, this[PLUGINS])),
119+
invalid: tests.invalid
120+
.map(stripTypeOnEslint10)
121+
.map((test) => convertToFlat(test, this[PLUGINS])),
102122
};
103123

104124
super.run(ruleName, rule, newTests);

0 commit comments

Comments
 (0)