Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(debug): add debug utility #11

Merged
merged 6 commits into from
May 11, 2024
Merged

feat(debug): add debug utility #11

merged 6 commits into from
May 11, 2024

Conversation

mcous
Copy link
Owner

@mcous mcous commented May 8, 2024

This PR adds the ability to debug a vitest-when mock:

import { when, debug } from 'vitest-when'

const coolFunc = vi.fn().mockName('coolFunc')

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

const result = coolFunc(1, 2, 4)

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

The debug function logs by default, but also returns an object that can be used by other tooling:

const debugInfo = debug(coolFunc, { log: false })
console.dir(debugInfo, { depth: 3 })
// {
//   name: 'coolFunc',
//   stubbings: [
//     {
//       args: [ 4, 5, 6 ],
//       behavior: {
//         type: 'throw',
//         error: Error: oh no
//             at /Users/mc/projects/vitest-when/test/vitest-when.test.ts:357:62
//       },
//       calls: []
//     },
//     {
//       args: [ 1, 2, 3 ],
//       behavior: { type: 'return', value: 123 },
//       calls: []
//     }
//   ],
//   unmatchedCalls: [ [ 1, 2, 4 ] ],
//   description: '`coolFunc()` has...'
// }

@mcous
Copy link
Owner Author

mcous commented May 8, 2024

@BeniRupp I've been giving some thought to #10, and I wonder if something like this debug interface, combined with a user-side mock registry, could be used to implement the feature you were after

@mcous mcous merged commit 9ac6268 into main May 11, 2024
22 checks passed
@mcous mcous deleted the debug branch May 11, 2024 16:09
@BeniRupp
Copy link

@mcous, that looks awesome! I think this debug function can be very helpful. Thanks for pushing that topic! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants