Skip to content

Commit

Permalink
test(utils): dedupe code between test and createRuleTestCaseFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Nov 10, 2024
1 parent 4056a17 commit 65a8118
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ export function testVersion<T extends ValidTestCase>(
return eslintVersionSatisfies(specifier) ? [test(t())] : []
}

/** @warning DO NOT EXPORT THIS. use {@link createRuleTestCaseFunction} or {@link test} instead */
function createRuleTestCase<TTestCase extends TSESLintValidTestCase<unknown[]>>(
t: TTestCase,
): TTestCase {
return {
filename: TEST_FILENAME,
...t,
languageOptions: {
...t.languageOptions,
parserOptions: {
sourceType: 'module',
ecmaVersion: 9,
...t.languageOptions?.parserOptions,
},
},
}
}

export type InvalidTestCaseError =
| string
| InvalidTestCase['errors'][number]
Expand All @@ -83,19 +101,8 @@ export function test<T extends ValidTestCase>(
): T extends { errors: InvalidTestCaseError[] | number }
? InvalidTestCase
: ValidTestCase {
// @ts-expect-error -- simplify testing
return {
filename: TEST_FILENAME,
...t,
languageOptions: {
...t.languageOptions,
parserOptions: {
sourceType: 'module',
ecmaVersion: 9,
...t.languageOptions?.parserOptions,
},
},
}
// @ts-expect-error simplify testing
return createRuleTestCase(t)
}

type GetRuleType<TRule> =
Expand All @@ -121,20 +128,8 @@ export function createRuleTestCaseFunction<
>(
t: TTestCase,
) => TReturn {
return t => {
return {
filename: TEST_FILENAME,
...t,
languageOptions: {
...t.languageOptions,
parserOptions: {
sourceType: 'module',
ecmaVersion: 9,
...t.languageOptions?.parserOptions,
},
},
} as never
}
// @ts-expect-error simplify testing
return createRuleTestCase
}

export function testContext(settings?: PluginSettings) {
Expand Down

0 comments on commit 65a8118

Please sign in to comment.