Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add helpful message when test case has non-string code/name #15425

Merged
merged 1 commit into from Dec 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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