From 05faebb943456ad2b20117f3c8b3eccbe2e2fb03 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sat, 21 Dec 2019 06:56:06 +1030 Subject: [PATCH] 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. --- lib/rule-tester/rule-tester.js | 6 +++-- tests/lib/rule-tester/rule-tester.js | 38 +++++++++++++++------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/rule-tester/rule-tester.js b/lib/rule-tester/rule-tester.js index b4b3b2bec86..44e01dadf09 100644 --- a/lib/rule-tester/rule-tester.js +++ b/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`); diff --git a/tests/lib/rule-tester/rule-tester.js b/tests/lib/rule-tester/rule-tester.js index 6ab341ccc7e..4abb053abd4 100644 --- a/tests/lib/rule-tester/rule-tester.js +++ b/tests/lib/rule-tester/rule-tester.js @@ -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", () => {