Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: improve suggestion testing experience (#12602)
Previously you had to explicitly pass the exact same value (undefined), which meant either explicitly set `suggestions: undefined`, or omit the property.
I hate writing `undefined`, but I prefer to be explicit in my tests and say "I expect no suggestions".
Now you can pass any falsey, or an empty array, and the rule tester will perform as expected.
  • Loading branch information
bradzacher authored and kaicataldo committed Dec 20, 2019
1 parent 05f7dd5 commit 05faebb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -596,8 +596,10 @@ class RuleTester {
if (hasOwnProperty(error, "suggestions")) {

// Support asserting there are no suggestions
if (!error.suggestions) {
assert.strictEqual(message.suggestions, error.suggestions, `Error should have no suggestions on error with message: "${message.message}"`);
if (!error.suggestions || (Array.isArray(error.suggestions) && error.suggestions.length === 0)) {
if (Array.isArray(message.suggestions) && message.suggestions.length > 0) {
assert.fail(`Error should have no suggestions on error with message: "${message.message}"`);
}
} else {
assert.strictEqual(Array.isArray(message.suggestions), true, `Error should have an array of suggestions. Instead received "${message.suggestions}" on error with message: "${message.message}"`);
assert.strictEqual(message.suggestions.length, error.suggestions.length, `Error should have ${error.suggestions.length} suggestions. Instead found ${message.suggestions.length} suggestions`);
Expand Down
38 changes: 21 additions & 17 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -1018,29 +1018,33 @@ describe("RuleTester", () => {
});

it("should support explicitly expecting no suggestions", () => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [],
invalid: [{
code: "eval('var foo');",
errors: [{
suggestions: void 0
}]
}]
});
});

it("should fail when expecting no suggestions and there are suggestions", () => {
assert.throws(() => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
[void 0, null, false, []].forEach(suggestions => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/no-eval"), {
valid: [],
invalid: [{
code: "var foo;",
code: "eval('var foo');",
errors: [{
suggestions: void 0
suggestions
}]
}]
});
}, "Error should have no suggestions on error with message: \"Avoid using identifiers named 'foo'.\"");
});
});

it("should fail when expecting no suggestions and there are suggestions", () => {
[void 0, null, false, []].forEach(suggestions => {
assert.throws(() => {
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
valid: [],
invalid: [{
code: "var foo;",
errors: [{
suggestions
}]
}]
});
}, "Error should have no suggestions on error with message: \"Avoid using identifiers named 'foo'.\"");
});
});

it("should fail when testing for suggestions that don't exist", () => {
Expand Down

0 comments on commit 05faebb

Please sign in to comment.