diff --git a/docs/developer-guide/nodejs-api.md b/docs/developer-guide/nodejs-api.md index 54f47c66500..378b9c16b8d 100644 --- a/docs/developer-guide/nodejs-api.md +++ b/docs/developer-guide/nodejs-api.md @@ -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). diff --git a/lib/rule-tester/rule-tester.js b/lib/rule-tester/rule-tester.js index 324af7b2cae..7f590a5ea70 100644 --- a/lib/rule-tester/rule-tester.js +++ b/lib/rule-tester/rule-tester.js @@ -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. @@ -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} 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. @@ -124,6 +126,7 @@ let defaultConfig = { rules: {} }; * configuration */ const RuleTesterParameters = [ + "name", "code", "filename", "options", @@ -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); } @@ -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); } diff --git a/tests/lib/rule-tester/rule-tester.js b/tests/lib/rule-tester/rule-tester.js index 0ef3dd00e70..6ebd82fb5ab 100644 --- a/tests/lib/rule-tester/rule-tester.js +++ b/tests/lib/rule-tester/rule-tester.js @@ -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; + }); + + 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