diff --git a/packages/rule-tester/src/RuleTester.ts b/packages/rule-tester/src/RuleTester.ts index cc6577611378..198309699af9 100644 --- a/packages/rule-tester/src/RuleTester.ts +++ b/packages/rule-tester/src/RuleTester.ts @@ -395,33 +395,37 @@ export class RuleTester extends TestFramework { * one of the templates above. */ constructor.describe(ruleName, () => { - constructor.describe('valid', () => { - normalizedTests.valid.forEach(valid => { - const testName = ((): string => { - if (valid.name == null || valid.name.length === 0) { - return valid.code; - } - return valid.name; - })(); - constructor[getTestMethod(valid)](sanitize(testName), () => { - this.#testValidTemplate(ruleName, rule, valid); + if (normalizedTests.valid.length) { + constructor.describe('valid', () => { + normalizedTests.valid.forEach(valid => { + const testName = ((): string => { + if (valid.name == null || valid.name.length === 0) { + return valid.code; + } + return valid.name; + })(); + constructor[getTestMethod(valid)](sanitize(testName), () => { + this.#testValidTemplate(ruleName, rule, valid); + }); }); }); - }); + } - constructor.describe('invalid', () => { - normalizedTests.invalid.forEach(invalid => { - const name = ((): string => { - if (invalid.name == null || invalid.name.length === 0) { - return invalid.code; - } - return invalid.name; - })(); - constructor[getTestMethod(invalid)](sanitize(name), () => { - this.#testInvalidTemplate(ruleName, rule, invalid); + if (normalizedTests.invalid.length) { + constructor.describe('invalid', () => { + normalizedTests.invalid.forEach(invalid => { + const name = ((): string => { + if (invalid.name == null || invalid.name.length === 0) { + return invalid.code; + } + return invalid.name; + })(); + constructor[getTestMethod(invalid)](sanitize(name), () => { + this.#testInvalidTemplate(ruleName, rule, invalid); + }); }); }); - }); + } }); } diff --git a/packages/rule-tester/tests/RuleTester.test.ts b/packages/rule-tester/tests/RuleTester.test.ts index 25b6aa0888ed..07ad0f31f8ab 100644 --- a/packages/rule-tester/tests/RuleTester.test.ts +++ b/packages/rule-tester/tests/RuleTester.test.ts @@ -819,6 +819,59 @@ describe('RuleTester', () => { expect(mockedDescribeSkip.mock.calls).toHaveLength(0); // expect(mockedIt.mock.lastCall).toMatchInlineSnapshot(`undefined`); }); + + it('does not call describe with valid if no valid tests are provided', () => { + const ruleTester = new RuleTester(); + + ruleTester.run('my-rule', NOOP_RULE, { + valid: [], + invalid: [ + { + code: 'invalid', + errors: [{ messageId: 'error' }], + }, + ], + }); + + expect(mockedDescribe.mock.calls).toMatchInlineSnapshot(` + [ + [ + "my-rule", + [Function], + ], + [ + "invalid", + [Function], + ], + ] + `); + }); + + it('does not call describe with invalid if no invalid tests are provided', () => { + const ruleTester = new RuleTester(); + + ruleTester.run('my-rule', NOOP_RULE, { + valid: [ + { + code: 'valid', + }, + ], + invalid: [], + }); + + expect(mockedDescribe.mock.calls).toMatchInlineSnapshot(` + [ + [ + "my-rule", + [Function], + ], + [ + "valid", + [Function], + ], + ] + `); + }); }); }); });