Skip to content

Commit

Permalink
feat: rule tester do not create empty valid or invalid test suites (#…
Browse files Browse the repository at this point in the history
…17475)

* feat: rule-tester do not create empty valid or invalid test suites

* chore: fix grammatical errors in test case descriptions

* fix: pass an invalid test case when checking for missing valid test cases

* chore: make test cases more consistent

* chore: actually make tests consistent
  • Loading branch information
DMartens authored Aug 21, 2023
1 parent ee2f718 commit d73fbf2
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 36 deletions.
42 changes: 24 additions & 18 deletions lib/rule-tester/flat-rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,29 +1011,35 @@ class FlatRuleTester {
/*
* This creates a mocha test suite and pipes all supplied info through
* one of the templates above.
* The test suites for valid/invalid are created conditionally as
* test runners (eg. vitest) fail for empty test suites.
*/
this.constructor.describe(ruleName, () => {
this.constructor.describe("valid", () => {
test.valid.forEach(valid => {
this.constructor[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
}
);
if (test.valid.length > 0) {
this.constructor.describe("valid", () => {
test.valid.forEach(valid => {
this.constructor[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
}
);
});
});
});
}

this.constructor.describe("invalid", () => {
test.invalid.forEach(invalid => {
this.constructor[invalid.only ? "itOnly" : "it"](
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
}
);
if (test.invalid.length > 0) {
this.constructor.describe("invalid", () => {
test.invalid.forEach(invalid => {
this.constructor[invalid.only ? "itOnly" : "it"](
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
}
);
});
});
});
}
});
}
}
Expand Down
42 changes: 24 additions & 18 deletions lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,29 +1021,35 @@ class RuleTester {
/*
* This creates a mocha test suite and pipes all supplied info through
* one of the templates above.
* The test suites for valid/invalid are created conditionally as
* test runners (eg. vitest) fail for empty test suites.
*/
this.constructor.describe(ruleName, () => {
this.constructor.describe("valid", () => {
test.valid.forEach(valid => {
this.constructor[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
}
);
if (test.valid.length > 0) {
this.constructor.describe("valid", () => {
test.valid.forEach(valid => {
this.constructor[valid.only ? "itOnly" : "it"](
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
() => {
testValidTemplate(valid);
}
);
});
});
});
}

this.constructor.describe("invalid", () => {
test.invalid.forEach(invalid => {
this.constructor[invalid.only ? "itOnly" : "it"](
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
}
);
if (test.invalid.length > 0) {
this.constructor.describe("invalid", () => {
test.invalid.forEach(invalid => {
this.constructor[invalid.only ? "itOnly" : "it"](
sanitize(invalid.name || invalid.code),
() => {
testInvalidTemplate(invalid);
}
);
});
});
});
}
});
}
}
Expand Down
69 changes: 69 additions & 0 deletions tests/lib/rule-tester/flat-rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2626,4 +2626,73 @@ describe("FlatRuleTester", () => {

});

describe("Optional Test Suites", () => {
let originalRuleTesterDescribe;
let spyRuleTesterDescribe;

before(() => {
originalRuleTesterDescribe = FlatRuleTester.describe;
spyRuleTesterDescribe = sinon.spy((title, callback) => callback());
FlatRuleTester.describe = spyRuleTesterDescribe;
});
after(() => {
FlatRuleTester.describe = originalRuleTesterDescribe;
});
beforeEach(() => {
spyRuleTesterDescribe.resetHistory();
ruleTester = new FlatRuleTester();
});

it("should create a test suite with the rule name even if there are no test cases", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: []
});
sinon.assert.calledWith(spyRuleTesterDescribe, "no-var");
});

it("should create a valid test suite if there is a valid test case", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["value = 0;"],
invalid: []
});
sinon.assert.calledWith(spyRuleTesterDescribe, "valid");
});

it("should not create a valid test suite if there are no valid test cases", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [
{
code: "var value = 0;",
errors: [/^Bad var/u],
output: " value = 0;"
}
]
});
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "valid");
});

it("should create an invalid test suite if there is an invalid test case", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [
{
code: "var value = 0;",
errors: [/^Bad var/u],
output: " value = 0;"
}
]
});
sinon.assert.calledWith(spyRuleTesterDescribe, "invalid");
});

it("should not create an invalid test suite if there are no invalid test cases", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["value = 0;"],
invalid: []
});
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "invalid");
});
});
});
69 changes: 69 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2870,4 +2870,73 @@ describe("RuleTester", () => {

});

describe("Optional Test Suites", () => {
let originalRuleTesterDescribe;
let spyRuleTesterDescribe;

before(() => {
originalRuleTesterDescribe = RuleTester.describe;
spyRuleTesterDescribe = sinon.spy((title, callback) => callback());
RuleTester.describe = spyRuleTesterDescribe;
});
after(() => {
RuleTester.describe = originalRuleTesterDescribe;
});
beforeEach(() => {
spyRuleTesterDescribe.resetHistory();
ruleTester = new RuleTester();
});

it("should create a test suite with the rule name even if there are no test cases", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: []
});
sinon.assert.calledWith(spyRuleTesterDescribe, "no-var");
});

it("should create a valid test suite if there is a valid test case", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["value = 0;"],
invalid: []
});
sinon.assert.calledWith(spyRuleTesterDescribe, "valid");
});

it("should not create a valid test suite if there are no valid test cases", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [
{
code: "var value = 0;",
errors: [/^Bad var/u],
output: " value = 0;"
}
]
});
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "valid");
});

it("should create an invalid test suite if there is an invalid test case", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: [],
invalid: [
{
code: "var value = 0;",
errors: [/^Bad var/u],
output: " value = 0;"
}
]
});
sinon.assert.calledWith(spyRuleTesterDescribe, "invalid");
});

it("should not create an invalid test suite if there are no invalid test cases", () => {
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
valid: ["value = 0;"],
invalid: []
});
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "invalid");
});
});
});

0 comments on commit d73fbf2

Please sign in to comment.