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

feat: rule tester do not create empty valid or invalid test suites #17475

Merged
merged 5 commits into from Aug 21, 2023
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
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) {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
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) {
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
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
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
DMartens marked this conversation as resolved.
Show resolved Hide resolved
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");
});
});
});