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

New: Add name to RuleTester #15179

Merged
merged 1 commit into from Oct 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/developer-guide/nodejs-api.md
Expand Up @@ -750,6 +750,7 @@ The `RuleTester#run()` method is used to run the tests. It should be passed the

A test case is an object with the following properties:

* `name` (string, optional): The name to use for the test case, to make it easier to find
* `code` (string, required): The source code that the rule should be run on
* `options` (array, optional): The options passed to the rule. The rule severity should not be included in this list.
* `filename` (string, optional): The filename for the given case (useful for rules that make assertions about filenames).
Expand Down
7 changes: 5 additions & 2 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -67,6 +67,7 @@ const { SourceCode } = require("../source-code");
/**
* A test case that is expected to pass lint.
* @typedef {Object} ValidTestCase
* @property {string} [name] Name for the test case.
* @property {string} code Code for the test case.
* @property {any[]} [options] Options for the test case.
* @property {{ [name: string]: any }} [settings] Settings for the test case.
Expand All @@ -81,6 +82,7 @@ const { SourceCode } = require("../source-code");
/**
* A test case that is expected to fail lint.
* @typedef {Object} InvalidTestCase
* @property {string} [name] Name for the test case.
* @property {string} code Code for the test case.
* @property {number | Array<TestCaseError | string | RegExp>} errors Expected errors.
* @property {string | null} [output] The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
Expand Down Expand Up @@ -124,6 +126,7 @@ let defaultConfig = { rules: {} };
* configuration
*/
const RuleTesterParameters = [
"name",
"code",
"filename",
"options",
Expand Down Expand Up @@ -964,7 +967,7 @@ class RuleTester {
RuleTester.describe("valid", () => {
test.valid.forEach(valid => {
RuleTester[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.code : valid),
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
}
Expand All @@ -975,7 +978,7 @@ class RuleTester {
RuleTester.describe("invalid", () => {
test.invalid.forEach(invalid => {
RuleTester[invalid.only ? "itOnly" : "it"](
sanitize(invalid.code),
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
}
Expand Down
53 changes: 53 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -2385,6 +2385,59 @@ describe("RuleTester", () => {

return assertion;
});

it('should use the "name" property if set to a non-empty string', () => {
const assertion = assertEmitted(ruleTesterTestEmitter, "it", "my test");

ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [
{
name: "my test",
code: "var x = invalid(code);",
output: " x = invalid(code);",
errors: 1
}
]
});

return assertion;
});
G-Rath marked this conversation as resolved.
Show resolved Hide resolved

it('should use the "name" property if set to a non-empty string for valid cases too', () => {
const assertion = assertEmitted(ruleTesterTestEmitter, "it", "my test");

ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [
{
name: "my test",
code: "valid(code);"
}
],
invalid: []
});

return assertion;
});


it('should use the test code as the name if the "name" property is set to an empty string', () => {
const assertion = assertEmitted(ruleTesterTestEmitter, "it", "var x = invalid(code);");

ruleTester.run("foo", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [
{
name: "",
code: "var x = invalid(code);",
output: " x = invalid(code);",
errors: 1
}
]
});

return assertion;
});
});

// https://github.com/eslint/eslint/issues/11615
Expand Down