Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: improve suggestion testing experience #12602

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
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)) {
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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