diff --git a/packages/utils/src/eslint-utils/rule-tester/RuleTester.ts b/packages/utils/src/eslint-utils/rule-tester/RuleTester.ts index 90c2f194386..9925ee2fd9f 100644 --- a/packages/utils/src/eslint-utils/rule-tester/RuleTester.ts +++ b/packages/utils/src/eslint-utils/rule-tester/RuleTester.ts @@ -50,6 +50,14 @@ interface RunTests< type AfterAll = (fn: () => void) => void; +function isDescribeWithSkip( + value: unknown, +): value is RuleTesterTestFrameworkFunction & { + skip: RuleTesterTestFrameworkFunction; +} { + return 'skip' in describe && typeof describe.skip === 'function'; +} + class RuleTester extends BaseRuleTester.RuleTester { readonly #baseOptions: RuleTesterConfig; @@ -134,17 +142,10 @@ class RuleTester extends BaseRuleTester.RuleTester { this.#baseOptions.dependencyConstraints, ) ) { - type DescribeWithMaybeSkip = RuleTesterTestFrameworkFunction & { - skip?: RuleTesterTestFrameworkFunction; - }; - const describe: DescribeWithMaybeSkip = this.staticThis.describe; - if ('skip' in describe && typeof describe.skip === 'function') { + if (isDescribeWithSkip(this.staticThis.describe)) { // for frameworks like mocha or jest that have a "skip" version of their function // we can provide a nice skipped test! - type DescribeWithSkip = RuleTesterTestFrameworkFunction & { - skip: RuleTesterTestFrameworkFunction; - }; - (this.staticThis.describe as DescribeWithSkip).skip(name, () => { + this.staticThis.describe.skip(name, () => { this.staticThis.it( 'All tests skipped due to unsatisfied constructor dependency constraints', () => {}, diff --git a/packages/utils/src/ts-eslint/RuleTester.ts b/packages/utils/src/ts-eslint/RuleTester.ts index 61855f4bbfe..6c0b98b795f 100644 --- a/packages/utils/src/ts-eslint/RuleTester.ts +++ b/packages/utils/src/ts-eslint/RuleTester.ts @@ -125,6 +125,10 @@ interface TestCaseError { // readonly message?: string | RegExp; } +/** + * @param text a string describing the rule + * @param callback the test callback + */ type RuleTesterTestFrameworkFunction = ( text: string, callback: () => void, @@ -166,8 +170,6 @@ declare class RuleTesterBase { /** * If you supply a value to this property, the rule tester will call this instead of using the version defined on * the global namespace. - * @param text a string describing the rule - * @param callback the test callback */ static get describe(): RuleTesterTestFrameworkFunction; static set describe(value: RuleTesterTestFrameworkFunction | undefined); @@ -175,8 +177,6 @@ declare class RuleTesterBase { /** * If you supply a value to this property, the rule tester will call this instead of using the version defined on * the global namespace. - * @param text a string describing the test case - * @param callback the test callback */ static get it(): RuleTesterTestFrameworkFunction; static set it(value: RuleTesterTestFrameworkFunction | undefined); @@ -184,16 +184,12 @@ declare class RuleTesterBase { /** * If you supply a value to this property, the rule tester will call this instead of using the version defined on * the global namespace. - * @param text a string describing the test case - * @param callback the test callback */ static get itOnly(): RuleTesterTestFrameworkFunction; static set itOnly(value: RuleTesterTestFrameworkFunction | undefined); /** * Define a rule for one particular run of tests. - * @param name The name of the rule to define. - * @param rule The rule definition. */ defineRule>( name: string, diff --git a/packages/utils/tests/eslint-utils/rule-tester/RuleTester.test.ts b/packages/utils/tests/eslint-utils/rule-tester/RuleTester.test.ts index a378e7e6106..2e620332942 100644 --- a/packages/utils/tests/eslint-utils/rule-tester/RuleTester.test.ts +++ b/packages/utils/tests/eslint-utils/rule-tester/RuleTester.test.ts @@ -739,15 +739,17 @@ describe('RuleTester', () => { ], }); - expect(mockedDescribe.mock.lastCall).toMatchInlineSnapshot(` + // trigger the describe block + expect(mockedDescribe.mock.calls.length).toBeGreaterThanOrEqual(1); + mockedDescribe.mock.lastCall?.[1](); + expect(mockedDescribe.mock.calls).toMatchInlineSnapshot(` [ - "my-rule", - [Function], + [ + "my-rule", + [Function], + ], ] `); - - // trigger the describe block - mockedDescribe.mock.lastCall?.[1](); expect(mockedIt.mock.lastCall).toMatchInlineSnapshot(` [ "All tests skipped due to unsatisfied constructor dependency constraints", @@ -777,6 +779,27 @@ describe('RuleTester', () => { }, ], }); + + // trigger the describe block + expect(mockedDescribe.mock.calls.length).toBeGreaterThanOrEqual(1); + mockedDescribe.mock.lastCall?.[1](); + expect(mockedDescribe.mock.calls).toMatchInlineSnapshot(` + [ + [ + "my-rule", + [Function], + ], + [ + "valid", + [Function], + ], + [ + "invalid", + [Function], + ], + ] + `); + // expect(mockedIt.mock.lastCall).toMatchInlineSnapshot(`undefined`); }); }); });