Skip to content

Commit

Permalink
feat(rule-tester): allow to create empty tests (#7467)
Browse files Browse the repository at this point in the history
* feat(rule-tester): allow to create empty tests

* feat(rule-tester): add tests with empty valid and invalid suites

* fix(rule-tester): improve tests with empty rule tester suites
  • Loading branch information
azat-io committed Sep 3, 2023
1 parent 7012279 commit 654b35d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
48 changes: 26 additions & 22 deletions packages/rule-tester/src/RuleTester.ts
Expand Up @@ -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);
});
});
});
});
}
});
}

Expand Down
53 changes: 53 additions & 0 deletions packages/rule-tester/tests/RuleTester.test.ts
Expand Up @@ -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],
],
]
`);
});
});
});
});

0 comments on commit 654b35d

Please sign in to comment.