Skip to content

Commit

Permalink
fix: add helpful message when test case has non-string code/name (#15425
Browse files Browse the repository at this point in the history
)

Fixes #13917
  • Loading branch information
bmish committed Dec 14, 2021
1 parent a53e59e commit f4559a0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -216,6 +216,9 @@ function freezeDeeply(x) {
* @returns {string} The sanitized text.
*/
function sanitize(text) {
if (typeof text !== "string") {
return "";
}
return text.replace(
/[\u0000-\u0009\u000b-\u001a]/gu, // eslint-disable-line no-control-regex -- Escaping controls
c => `\\u${c.codePointAt(0).toString(16).padStart(4, "0")}`
Expand Down Expand Up @@ -691,6 +694,13 @@ class RuleTester {
* @private
*/
function testValidTemplate(item) {
const code = typeof item === "object" ? item.code : item;

assert.ok(typeof code === "string", "Test case must specify a string value for 'code'");
if (item.name) {
assert.ok(typeof item.name === "string", "Optional test case property 'name' must be a string");
}

const result = runRuleForItem(item);
const messages = result.messages;

Expand Down Expand Up @@ -731,6 +741,10 @@ class RuleTester {
* @private
*/
function testInvalidTemplate(item) {
assert.ok(typeof item.code === "string", "Test case must specify a string value for 'code'");
if (item.name) {
assert.ok(typeof item.name === "string", "Optional test case property 'name' must be a string");
}
assert.ok(item.errors || item.errors === 0,
`Did not specify errors for an invalid test of ${ruleName}`);

Expand Down
59 changes: 59 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -2441,6 +2441,65 @@ describe("RuleTester", () => {

return assertion;
});

it('should throw if "name" property is not a string', () => {
assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [{ code: "foo", name: 123 }],
invalid: [{ code: "foo" }]

});
}, /Optional test case property 'name' must be a string/u);

assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["foo"],
invalid: [{ code: "foo", name: 123 }]
});
}, /Optional test case property 'name' must be a string/u);
});

it('should throw if "code" property is not a string', () => {
assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [{ code: 123 }],
invalid: [{ code: "foo" }]

});
}, /Test case must specify a string value for 'code'/u);

assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [123],
invalid: [{ code: "foo" }]

});
}, /Test case must specify a string value for 'code'/u);

assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["foo"],
invalid: [{ code: 123 }]
});
}, /Test case must specify a string value for 'code'/u);
});

it('should throw if "code" property is missing', () => {
assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [{ }],
invalid: [{ code: "foo" }]

});
}, /Test case must specify a string value for 'code'/u);

assert.throws(() => {
ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["foo"],
invalid: [{ }]
});
}, /Test case must specify a string value for 'code'/u);
});
});

// https://github.com/eslint/eslint/issues/11615
Expand Down

0 comments on commit f4559a0

Please sign in to comment.