Skip to content

Commit

Permalink
fixup: typos and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed May 8, 2024
1 parent 4ded574 commit 881e864
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ expect(spy('hello')).toEqual('world')
expect(spy('hello')).toEqual('solar system')
```

### `debug(spy: TFunc, options?: DebugsOptions): DebugInfo`
### `debug(spy: TFunc, options?: DebugOptions): DebugInfo`

Logs and returns information about a mock's stubbing and usage. Useful if a test with mocks is failing and you can't figure out why.

Expand All @@ -481,13 +481,13 @@ when(coolFunc).calledWith(4, 5, 6).thenThrow(new Error('oh no'))
const result = coolFunc(1, 2, 4)

debug(coolFunc)
// `coolFunc` has:
// `coolFunc()` has:
//
// 2 stubbings with 0 calls:
// - `coolFunc(1, 2, 4)` -> `return 123`, called 0 times.
// - `coolFunc(4, 5, 6)` -> `throw Error('oh no)`, called 0 times.
// 2 stubbings with 0 calls
// - 0 calls: `coolFunc(4, 5, 6) => { throw [Error: oh no] }`
// - 0 calls: `coolFunc(1, 2, 3) => 123`
//
// 1 unmatched call:
// 1 unmatched call
// - `coolFunc(1, 2, 4)`
```

Expand Down
8 changes: 4 additions & 4 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const formatDebug = (debug: DebugResult): string => {
const unmatchedCallsCount = unmatchedCalls.length

return [
`\`${name}\` has:`,
`\`${name}()\` has:`,
'',
`${stubbingCount} ${plural(
'stubbing',
Expand All @@ -63,9 +63,9 @@ const formatStubbing = (
name: string,
{ args, behavior, calls }: Stubbing,
): string => {
return `\`${formatCall(name, args)} ${formatBehavior(behavior)}\`, called ${
calls.length
} times`
return `${calls.length} calls: \`${formatCall(name, args)} ${formatBehavior(
behavior,
)}\``
}

const formatCall = (name: string, args: readonly unknown[]): string => {
Expand Down
11 changes: 11 additions & 0 deletions test/vitest-when.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,15 @@ describe('vitest-when', () => {
unmatchedCalls: [],
})
})

it('logs debug description', () => {
const coolFunction = vi.fn().mockName('coolFunc')

subject.when(coolFunction).calledWith(1, 2, 3).thenReturn(123)
subject.when(coolFunction).calledWith(4, 5, 6).thenThrow(new Error('oh no'))

coolFunction(1, 2, 4)

subject.debug(coolFunction)
})
})

0 comments on commit 881e864

Please sign in to comment.