Skip to content
Permalink
Browse files

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 05faebb943456ad2b20117f3c8b3eccbe2e2fb03
Showing with 25 additions and 19 deletions.
  1. +4 −2 lib/rule-tester/rule-tester.js
  2. +21 −17 tests/lib/rule-tester/rule-tester.js
@@ -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`);
@@ -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", () => {

0 comments on commit 05faebb

Please sign in to comment.
You can’t perform that action at this time.