Skip to content

Commit

Permalink
feat: support output: null
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 6, 2024
1 parent 1a453ba commit 7d8f2a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ run('rule-name', rule, {
{
input: 'let foo = 1',
output(output) {
expect(output.slice(0, 3)).toBe('let')
expect(output)
.toMatchInlineSnapshot(`"const foo = 1;"`)
// Any custom assertion...
},
},
],
Expand All @@ -82,8 +84,10 @@ run('rule-name', rule, {
{
input: 'let foo = 1',
errors(errors) {
expect(errors).toHaveLength(1)
expect(errors.map(e => e.messageId))
.toMatchInlineSnapshot(`["error-message-id"]`)
// Any custom assertion...
},
},
],
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ export function createRuleTester(options: RuleTesterOptions): RuleTester {
}
result.messages = messages

if (_case.output) {
if (typeof _case.output === 'function')
_case.output(result.output!)
if (_case.output !== undefined) {
if (_case.output === null) // null means the output should be the same as the input
expect(result.output, 'output').toBe(_case.code)
else if (typeof _case.output === 'function') // custom assertion
_case.output(result.output!, _case.code)
else
expect(result.output, 'output').toBe(_case.output)
}
Expand Down
12 changes: 11 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ export interface ValidTestCaseBase extends Linter.FlatConfig {
}

export interface InvalidTestCaseBase extends ValidTestCaseBase {
/**
* Expected errors.
* If a number is provided, it asserts that the number of errors is equal to the number provided.
* If an array of strings is provided, it asserts that the error messageIds are equal to the array provided.
* If an array of objects is provided, it asserts that the errors are partially equal to the objects provided.
*/
errors?: number | (string | Partial<Linter.LintMessage>)[] | ((errors: Linter.LintMessage[]) => void)
output?: string | ((output: string) => void)
/**
* Assert if output is expected.
* Pass `null` to assert that the output is the same as the input.
*/
output?: string | null | ((output: string, input: string) => void)
}

export interface NormalizedTestCase extends InvalidTestCaseBase {
Expand Down

0 comments on commit 7d8f2a5

Please sign in to comment.