Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Oct 31, 2022
1 parent e220691 commit 3e85184
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
19 changes: 10 additions & 9 deletions packages/utils/src/eslint-utils/rule-tester/RuleTester.ts
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
() => {},
Expand Down
12 changes: 4 additions & 8 deletions packages/utils/src/ts-eslint/RuleTester.ts
Expand Up @@ -125,6 +125,10 @@ interface TestCaseError<TMessageIds extends string> {
// readonly message?: string | RegExp;
}

/**
* @param text a string describing the rule
* @param callback the test callback
*/
type RuleTesterTestFrameworkFunction = (
text: string,
callback: () => void,
Expand Down Expand Up @@ -166,34 +170,26 @@ 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);

/**
* 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);

/**
* 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<TMessageIds extends string, TOptions extends Readonly<unknown[]>>(
name: string,
Expand Down
35 changes: 29 additions & 6 deletions packages/utils/tests/eslint-utils/rule-tester/RuleTester.test.ts
Expand Up @@ -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",
Expand Down Expand Up @@ -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`);
});
});
});
Expand Down

0 comments on commit 3e85184

Please sign in to comment.