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
  • Loading branch information
DMartens committed Aug 17, 2023
1 parent 42faa17 commit 9ea3f3c
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 36 deletions.
42 changes: 24 additions & 18 deletions lib/rule-tester/flat-rule-tester.js
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
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
63 changes: 63 additions & 0 deletions tests/lib/rule-tester/flat-rule-tester.js
Expand Up @@ -2626,4 +2626,67 @@ 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 are is a valid test cases", () => {
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: []
});
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "valid");
});

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

it("should not create a 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");
});
});
});
63 changes: 63 additions & 0 deletions tests/lib/rule-tester/rule-tester.js
Expand Up @@ -2870,4 +2870,67 @@ 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 are is a valid test cases", () => {
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: []
});
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "valid");
});

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

it("should not create a 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 9ea3f3c

Please sign in to comment.