Skip to content
Permalink
Browse files
New: Add name to RuleTester (#15179)
  • Loading branch information
G-Rath committed Oct 21, 2021
1 parent 90a5b6b commit e926b1735c77bf55abc1150b060a535a6c4e2778
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
@@ -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).
@@ -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<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.
@@ -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);
}
@@ -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

0 comments on commit e926b17

Please sign in to comment.