diff --git a/README.md b/README.md index 41aec98..3888ace 100644 --- a/README.md +++ b/README.md @@ -502,16 +502,16 @@ import type { DebugOptions } from 'vitest-when' #### `DebugResult` ```ts -import type { DebugResult, DebugStubbing, DebugBehavior } from 'vitest-when' +import type { DebugResult, Stubbing, Behavior } from 'vitest-when' ``` | fields | type | description | | ---------------------------- | -------------------------------------------- | ----------------------------------------------------------- | | `description` | `string` | A human-readable description of the stub, logged by default | | `name` | `string` | The name of the mock, if set by [`mockName`][mockName] | -| `stubbings` | `DebugStubbing[]` | The list of configured stub behaviors | +| `stubbings` | `Stubbing[]` | The list of configured stub behaviors | | `stubbings[].args` | `unknown[]` | The stubbing's arguments to match | -| `stubbings[].behavior` | `DebugBehavior` | The configured behavior of the stubbing | +| `stubbings[].behavior` | `Behavior` | The configured behavior of the stubbing | | `stubbings[].behavior.type` | `return`, `throw`, `resolve`, `reject`, `do` | Result type of the stubbing | | `stubbings[].behavior.value` | `unknown` | Value for the behavior, if `type` is `return` or `resolve` | | `stubbings[].behavior.error` | `unknown` | Error for the behavior, it `type` is `throw` or `reject` | diff --git a/src/behaviors.ts b/src/behaviors.ts index 10fb77a..96cf7df 100644 --- a/src/behaviors.ts +++ b/src/behaviors.ts @@ -33,12 +33,20 @@ export interface BehaviorEntry { maxCallCount?: number | undefined } +export const BehaviorType = { + RETURN: 'return', + RESOLVE: 'resolve', + THROW: 'throw', + REJECT: 'reject', + DO: 'do', +} as const + export type Behavior = - | { type: 'return'; value: unknown } - | { type: 'resolve'; value: unknown } - | { type: 'throw'; error: unknown } - | { type: 'reject'; error: unknown } - | { type: 'do'; callback: AnyFunction } + | { type: typeof BehaviorType.RETURN; value: unknown } + | { type: typeof BehaviorType.RESOLVE; value: unknown } + | { type: typeof BehaviorType.THROW; error: unknown } + | { type: typeof BehaviorType.REJECT; error: unknown } + | { type: typeof BehaviorType.DO; callback: AnyFunction } export interface BehaviorOptions { value: TValue @@ -77,7 +85,7 @@ export const createBehaviorStack = < ({ value, maxCallCount }) => ({ args, maxCallCount, - behavior: { type: 'return' as const, value }, + behavior: { type: BehaviorType.RETURN, value }, calls: [], }), ), @@ -89,7 +97,7 @@ export const createBehaviorStack = < ({ value, maxCallCount }) => ({ args, maxCallCount, - behavior: { type: 'resolve' as const, value }, + behavior: { type: BehaviorType.RESOLVE, value }, calls: [], }), ), @@ -101,7 +109,7 @@ export const createBehaviorStack = < ({ value, maxCallCount }) => ({ args, maxCallCount, - behavior: { type: 'throw' as const, error: value }, + behavior: { type: BehaviorType.THROW, error: value }, calls: [], }), ), @@ -113,7 +121,7 @@ export const createBehaviorStack = < ({ value, maxCallCount }) => ({ args, maxCallCount, - behavior: { type: 'reject' as const, error: value }, + behavior: { type: BehaviorType.REJECT, error: value }, calls: [], }), ), @@ -125,7 +133,7 @@ export const createBehaviorStack = < ({ value, maxCallCount }) => ({ args, maxCallCount, - behavior: { type: 'do' as const, callback: value }, + behavior: { type: BehaviorType.DO, callback: value }, calls: [], }), ), diff --git a/src/debug.ts b/src/debug.ts index 1b25815..1d1fb60 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -5,7 +5,7 @@ import { import { validateSpy, getBehaviorStack } from './stubs' import type { AnyFunction } from './types' -import type { Behavior } from './behaviors' +import { type Behavior, BehaviorType } from './behaviors' export interface DebugResult { name: string @@ -71,23 +71,23 @@ const formatCall = (args: readonly unknown[]): string => { const formatBehavior = (behavior: Behavior): string => { switch (behavior.type) { - case 'return': { + case BehaviorType.RETURN: { return `=> ${stringify(behavior.value)}` } - case 'resolve': { + case BehaviorType.RESOLVE: { return `=> Promise.resolve(${stringify(behavior.value)})` } - case 'throw': { + case BehaviorType.THROW: { return `=> { throw ${stringify(behavior.error)} }` } - case 'reject': { + case BehaviorType.REJECT: { return `=> Promise.reject(${stringify(behavior.error)})` } - case 'do': { + case BehaviorType.DO: { return `=> ${stringify(behavior.callback)}()` } } diff --git a/src/stubs.ts b/src/stubs.ts index 750f748..05adad0 100644 --- a/src/stubs.ts +++ b/src/stubs.ts @@ -1,5 +1,9 @@ import { type Mock as Spy } from 'vitest' -import { createBehaviorStack, type BehaviorStack } from './behaviors.ts' +import { + createBehaviorStack, + type BehaviorStack, + BehaviorType, +} from './behaviors.ts' import { NotAMockFunctionError } from './errors.ts' import type { AnyFunction } from './types.ts' @@ -24,28 +28,28 @@ export const configureStub = ( const implementation = (...args: Parameters): unknown => { const behavior = behaviors.use(args)?.behavior ?? { - type: 'return', + type: BehaviorType.RETURN, value: undefined, } switch (behavior.type) { - case 'return': { + case BehaviorType.RETURN: { return behavior.value } - case 'resolve': { + case BehaviorType.RESOLVE: { return Promise.resolve(behavior.value) } - case 'throw': { + case BehaviorType.THROW: { throw behavior.error } - case 'reject': { + case BehaviorType.REJECT: { return Promise.reject(behavior.error) } - case 'do': { + case BehaviorType.DO: { return behavior.callback(...args) } } diff --git a/src/vitest-when.ts b/src/vitest-when.ts index f562254..f582e7f 100644 --- a/src/vitest-when.ts +++ b/src/vitest-when.ts @@ -3,7 +3,8 @@ import type { WhenOptions } from './behaviors.ts' import type { AnyFunction } from './types.ts' import { getDebug, type DebugResult } from './debug.ts' -export type { WhenOptions } from './behaviors.ts' +export { type WhenOptions, type Behavior, BehaviorType } from './behaviors.ts' +export type { DebugResult, Stubbing } from './debug.ts' export * from './errors.ts' export interface StubWrapper { @@ -49,9 +50,10 @@ export const debug = ( spy: TFunc, options: DebugOptions = {}, ): DebugResult => { + const log = options.log ?? true const result = getDebug(spy) - if (options.log !== false) { + if (log) { console.debug(result.description) }