Skip to content

Commit

Permalink
feat: support output to be a function
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 6, 2024
1 parent 340a766 commit 318c75a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ export function createRuleTester(options: RuleTesterOptions): RuleTesterResult {

if (_case.errors) {
if (typeof _case.errors === 'number') {
expect.soft(messages.length).toBe(_case.errors)
expect.soft(messages.length, 'number of error messages').toBe(_case.errors)
}
else {
const errors = Array.isArray(_case.errors) ? _case.errors : [_case.errors]
expect.soft(messages.length).toBe(errors.length)
expect(messages.length, 'number of error messages').toBe(errors.length)

errors.forEach((e, i) => {
if (typeof e === 'string')
expect.soft(messages[i].messageId, 'error massage ids').toBe(e)
expect.soft(messages[i].messageId, 'error massage id').toBe(e)
else
expect.soft(messages[i]).toMatchObject(e)
expect.soft(messages[i], 'error massage object').toMatchObject(e)
})
}
}
Expand All @@ -117,8 +117,12 @@ export function createRuleTester(options: RuleTesterOptions): RuleTesterResult {
}
result.messages = messages

if (_case.output)
expect.soft(result.output).toBe(_case.output)
if (_case.output) {
if (typeof _case.output === 'function')
_case.output(result.output!)
else
expect(result.output, 'output').toBe(_case.output)
}

if (verifyAfterFix && result.fixed) {
const messages = linter.verify(
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface ValidTestCaseBase extends Linter.FlatConfig {

export interface InvalidTestCaseBase extends ValidTestCaseBase {
errors?: number | (string | Partial<Linter.LintMessage>)[]
output?: string
output?: string | ((output: string) => void)
}

export interface NormalizedTestCase extends InvalidTestCaseBase {
Expand Down

0 comments on commit 318c75a

Please sign in to comment.