Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: added empty error array check for false negative (#13200)
* Update: added empty error array check for false negative

* Chore: message refactore

* Chore: fixed tests

* Update: throw error for errors: 0 in test files

* Update: changed error message

* Chore: changed assertion message
  • Loading branch information
anikethsaha committed Jun 17, 2020
1 parent 7fb45cf commit d98152a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -644,13 +644,22 @@ class RuleTester {
assert.ok(item.errors || item.errors === 0,
`Did not specify errors for an invalid test of ${ruleName}`);

if (Array.isArray(item.errors) && item.errors.length === 0) {
assert.fail("Invalid cases must have at least one error");
}

const ruleHasMetaMessages = hasOwnProperty(rule, "meta") && hasOwnProperty(rule.meta, "messages");
const friendlyIDList = ruleHasMetaMessages ? `[${Object.keys(rule.meta.messages).map(key => `'${key}'`).join(", ")}]` : null;

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

if (typeof item.errors === "number") {

if (item.errors === 0) {
assert.fail("Invalid cases must have 'error' value greater than 0");
}

assert.strictEqual(messages.length, item.errors, util.format("Should have %d error%s but had %d: %s",
item.errors, item.errors === 1 ? "" : "s", messages.length, util.inspect(messages)));
} else {
Expand Down
34 changes: 33 additions & 1 deletion tests/lib/rule-tester/rule-tester.js
Expand Up @@ -463,6 +463,37 @@ describe("RuleTester", () => {
}, expectedErrorMessage);
});

it("should throw error for empty error array", () => {
assert.throws(() => {
ruleTester.run("suggestions-messageIds", require("../../fixtures/testers/rule-tester/suggestions").withMessageIds, {
valid: [],
invalid: [{
code: "var foo;",
errors: []
}]
});
}, /Invalid cases must have at least one error/u);
});

it("should throw error for errors : 0", () => {
assert.throws(() => {
ruleTester.run(
"suggestions-messageIds",
require("../../fixtures/testers/rule-tester/suggestions")
.withMessageIds,
{
valid: [],
invalid: [
{
code: "var foo;",
errors: 0
}
]
}
);
}, /Invalid cases must have 'error' value greater than 0/u);
});

it("should not skip column assertion if column is a falsy value", () => {
assert.throws(() => {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
Expand Down Expand Up @@ -732,7 +763,7 @@ describe("RuleTester", () => {
{
code: "eval(foo)",
parser: require.resolve("esprima"),
errors: [{}]
errors: [{ line: 1 }]
}
]
});
Expand Down Expand Up @@ -1893,4 +1924,5 @@ describe("RuleTester", () => {
});

});

});

0 comments on commit d98152a

Please sign in to comment.