Skip to content

Commit d73fbf2

Browse files
authoredAug 21, 2023
feat: rule tester do not create empty valid or invalid test suites (#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
1 parent ee2f718 commit d73fbf2

File tree

4 files changed

+186
-36
lines changed

4 files changed

+186
-36
lines changed
 

‎lib/rule-tester/flat-rule-tester.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -1011,29 +1011,35 @@ class FlatRuleTester {
10111011
/*
10121012
* This creates a mocha test suite and pipes all supplied info through
10131013
* one of the templates above.
1014+
* The test suites for valid/invalid are created conditionally as
1015+
* test runners (eg. vitest) fail for empty test suites.
10141016
*/
10151017
this.constructor.describe(ruleName, () => {
1016-
this.constructor.describe("valid", () => {
1017-
test.valid.forEach(valid => {
1018-
this.constructor[valid.only ? "itOnly" : "it"](
1019-
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
1020-
() => {
1021-
testValidTemplate(valid);
1022-
}
1023-
);
1018+
if (test.valid.length > 0) {
1019+
this.constructor.describe("valid", () => {
1020+
test.valid.forEach(valid => {
1021+
this.constructor[valid.only ? "itOnly" : "it"](
1022+
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
1023+
() => {
1024+
testValidTemplate(valid);
1025+
}
1026+
);
1027+
});
10241028
});
1025-
});
1029+
}
10261030

1027-
this.constructor.describe("invalid", () => {
1028-
test.invalid.forEach(invalid => {
1029-
this.constructor[invalid.only ? "itOnly" : "it"](
1030-
sanitize(invalid.name || invalid.code),
1031-
() => {
1032-
testInvalidTemplate(invalid);
1033-
}
1034-
);
1031+
if (test.invalid.length > 0) {
1032+
this.constructor.describe("invalid", () => {
1033+
test.invalid.forEach(invalid => {
1034+
this.constructor[invalid.only ? "itOnly" : "it"](
1035+
sanitize(invalid.name || invalid.code),
1036+
() => {
1037+
testInvalidTemplate(invalid);
1038+
}
1039+
);
1040+
});
10351041
});
1036-
});
1042+
}
10371043
});
10381044
}
10391045
}

‎lib/rule-tester/rule-tester.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -1021,29 +1021,35 @@ class RuleTester {
10211021
/*
10221022
* This creates a mocha test suite and pipes all supplied info through
10231023
* one of the templates above.
1024+
* The test suites for valid/invalid are created conditionally as
1025+
* test runners (eg. vitest) fail for empty test suites.
10241026
*/
10251027
this.constructor.describe(ruleName, () => {
1026-
this.constructor.describe("valid", () => {
1027-
test.valid.forEach(valid => {
1028-
this.constructor[valid.only ? "itOnly" : "it"](
1029-
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
1030-
() => {
1031-
testValidTemplate(valid);
1032-
}
1033-
);
1028+
if (test.valid.length > 0) {
1029+
this.constructor.describe("valid", () => {
1030+
test.valid.forEach(valid => {
1031+
this.constructor[valid.only ? "itOnly" : "it"](
1032+
sanitize(typeof valid === "object" ? valid.name || valid.code : valid),
1033+
() => {
1034+
testValidTemplate(valid);
1035+
}
1036+
);
1037+
});
10341038
});
1035-
});
1039+
}
10361040

1037-
this.constructor.describe("invalid", () => {
1038-
test.invalid.forEach(invalid => {
1039-
this.constructor[invalid.only ? "itOnly" : "it"](
1040-
sanitize(invalid.name || invalid.code),
1041-
() => {
1042-
testInvalidTemplate(invalid);
1043-
}
1044-
);
1041+
if (test.invalid.length > 0) {
1042+
this.constructor.describe("invalid", () => {
1043+
test.invalid.forEach(invalid => {
1044+
this.constructor[invalid.only ? "itOnly" : "it"](
1045+
sanitize(invalid.name || invalid.code),
1046+
() => {
1047+
testInvalidTemplate(invalid);
1048+
}
1049+
);
1050+
});
10451051
});
1046-
});
1052+
}
10471053
});
10481054
}
10491055
}

‎tests/lib/rule-tester/flat-rule-tester.js

+69
Original file line numberDiff line numberDiff line change
@@ -2626,4 +2626,73 @@ describe("FlatRuleTester", () => {
26262626

26272627
});
26282628

2629+
describe("Optional Test Suites", () => {
2630+
let originalRuleTesterDescribe;
2631+
let spyRuleTesterDescribe;
2632+
2633+
before(() => {
2634+
originalRuleTesterDescribe = FlatRuleTester.describe;
2635+
spyRuleTesterDescribe = sinon.spy((title, callback) => callback());
2636+
FlatRuleTester.describe = spyRuleTesterDescribe;
2637+
});
2638+
after(() => {
2639+
FlatRuleTester.describe = originalRuleTesterDescribe;
2640+
});
2641+
beforeEach(() => {
2642+
spyRuleTesterDescribe.resetHistory();
2643+
ruleTester = new FlatRuleTester();
2644+
});
2645+
2646+
it("should create a test suite with the rule name even if there are no test cases", () => {
2647+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2648+
valid: [],
2649+
invalid: []
2650+
});
2651+
sinon.assert.calledWith(spyRuleTesterDescribe, "no-var");
2652+
});
2653+
2654+
it("should create a valid test suite if there is a valid test case", () => {
2655+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2656+
valid: ["value = 0;"],
2657+
invalid: []
2658+
});
2659+
sinon.assert.calledWith(spyRuleTesterDescribe, "valid");
2660+
});
2661+
2662+
it("should not create a valid test suite if there are no valid test cases", () => {
2663+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2664+
valid: [],
2665+
invalid: [
2666+
{
2667+
code: "var value = 0;",
2668+
errors: [/^Bad var/u],
2669+
output: " value = 0;"
2670+
}
2671+
]
2672+
});
2673+
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "valid");
2674+
});
2675+
2676+
it("should create an invalid test suite if there is an invalid test case", () => {
2677+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2678+
valid: [],
2679+
invalid: [
2680+
{
2681+
code: "var value = 0;",
2682+
errors: [/^Bad var/u],
2683+
output: " value = 0;"
2684+
}
2685+
]
2686+
});
2687+
sinon.assert.calledWith(spyRuleTesterDescribe, "invalid");
2688+
});
2689+
2690+
it("should not create an invalid test suite if there are no invalid test cases", () => {
2691+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2692+
valid: ["value = 0;"],
2693+
invalid: []
2694+
});
2695+
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "invalid");
2696+
});
2697+
});
26292698
});

‎tests/lib/rule-tester/rule-tester.js

+69
Original file line numberDiff line numberDiff line change
@@ -2870,4 +2870,73 @@ describe("RuleTester", () => {
28702870

28712871
});
28722872

2873+
describe("Optional Test Suites", () => {
2874+
let originalRuleTesterDescribe;
2875+
let spyRuleTesterDescribe;
2876+
2877+
before(() => {
2878+
originalRuleTesterDescribe = RuleTester.describe;
2879+
spyRuleTesterDescribe = sinon.spy((title, callback) => callback());
2880+
RuleTester.describe = spyRuleTesterDescribe;
2881+
});
2882+
after(() => {
2883+
RuleTester.describe = originalRuleTesterDescribe;
2884+
});
2885+
beforeEach(() => {
2886+
spyRuleTesterDescribe.resetHistory();
2887+
ruleTester = new RuleTester();
2888+
});
2889+
2890+
it("should create a test suite with the rule name even if there are no test cases", () => {
2891+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2892+
valid: [],
2893+
invalid: []
2894+
});
2895+
sinon.assert.calledWith(spyRuleTesterDescribe, "no-var");
2896+
});
2897+
2898+
it("should create a valid test suite if there is a valid test case", () => {
2899+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2900+
valid: ["value = 0;"],
2901+
invalid: []
2902+
});
2903+
sinon.assert.calledWith(spyRuleTesterDescribe, "valid");
2904+
});
2905+
2906+
it("should not create a valid test suite if there are no valid test cases", () => {
2907+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2908+
valid: [],
2909+
invalid: [
2910+
{
2911+
code: "var value = 0;",
2912+
errors: [/^Bad var/u],
2913+
output: " value = 0;"
2914+
}
2915+
]
2916+
});
2917+
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "valid");
2918+
});
2919+
2920+
it("should create an invalid test suite if there is an invalid test case", () => {
2921+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2922+
valid: [],
2923+
invalid: [
2924+
{
2925+
code: "var value = 0;",
2926+
errors: [/^Bad var/u],
2927+
output: " value = 0;"
2928+
}
2929+
]
2930+
});
2931+
sinon.assert.calledWith(spyRuleTesterDescribe, "invalid");
2932+
});
2933+
2934+
it("should not create an invalid test suite if there are no invalid test cases", () => {
2935+
ruleTester.run("no-var", require("../../fixtures/testers/rule-tester/no-var"), {
2936+
valid: ["value = 0;"],
2937+
invalid: []
2938+
});
2939+
sinon.assert.neverCalledWith(spyRuleTesterDescribe, "invalid");
2940+
});
2941+
});
28732942
});

0 commit comments

Comments
 (0)
Please sign in to comment.