Skip to content

Commit 05faebb

Browse files
bradzacherKai Cataldo
authored andcommitted
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.
1 parent 05f7dd5 commit 05faebb

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

lib/rule-tester/rule-tester.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,10 @@ class RuleTester {
596596
if (hasOwnProperty(error, "suggestions")) {
597597

598598
// Support asserting there are no suggestions
599-
if (!error.suggestions) {
600-
assert.strictEqual(message.suggestions, error.suggestions, `Error should have no suggestions on error with message: "${message.message}"`);
599+
if (!error.suggestions || (Array.isArray(error.suggestions) && error.suggestions.length === 0)) {
600+
if (Array.isArray(message.suggestions) && message.suggestions.length > 0) {
601+
assert.fail(`Error should have no suggestions on error with message: "${message.message}"`);
602+
}
601603
} else {
602604
assert.strictEqual(Array.isArray(message.suggestions), true, `Error should have an array of suggestions. Instead received "${message.suggestions}" on error with message: "${message.message}"`);
603605
assert.strictEqual(message.suggestions.length, error.suggestions.length, `Error should have ${error.suggestions.length} suggestions. Instead found ${message.suggestions.length} suggestions`);

tests/lib/rule-tester/rule-tester.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,29 +1018,33 @@ describe("RuleTester", () => {
10181018
});
10191019

10201020
it("should support explicitly expecting no suggestions", () => {
1021-
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/no-eval"), {
1022-
valid: [],
1023-
invalid: [{
1024-
code: "eval('var foo');",
1025-
errors: [{
1026-
suggestions: void 0
1027-
}]
1028-
}]
1029-
});
1030-
});
1031-
1032-
it("should fail when expecting no suggestions and there are suggestions", () => {
1033-
assert.throws(() => {
1034-
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
1021+
[void 0, null, false, []].forEach(suggestions => {
1022+
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/no-eval"), {
10351023
valid: [],
10361024
invalid: [{
1037-
code: "var foo;",
1025+
code: "eval('var foo');",
10381026
errors: [{
1039-
suggestions: void 0
1027+
suggestions
10401028
}]
10411029
}]
10421030
});
1043-
}, "Error should have no suggestions on error with message: \"Avoid using identifiers named 'foo'.\"");
1031+
});
1032+
});
1033+
1034+
it("should fail when expecting no suggestions and there are suggestions", () => {
1035+
[void 0, null, false, []].forEach(suggestions => {
1036+
assert.throws(() => {
1037+
ruleTester.run("suggestions-basic", require("../../fixtures/testers/rule-tester/suggestions").basic, {
1038+
valid: [],
1039+
invalid: [{
1040+
code: "var foo;",
1041+
errors: [{
1042+
suggestions
1043+
}]
1044+
}]
1045+
});
1046+
}, "Error should have no suggestions on error with message: \"Avoid using identifiers named 'foo'.\"");
1047+
});
10441048
});
10451049

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

0 commit comments

Comments
 (0)