Skip to content

Commit

Permalink
fixup: ensure public types are exported
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed May 11, 2024
1 parent 6fd155f commit e5194d6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
28 changes: 18 additions & 10 deletions src/behaviors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export interface BehaviorEntry<TArgs extends unknown[]> {
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<TValue> {
value: TValue
Expand Down Expand Up @@ -77,7 +85,7 @@ export const createBehaviorStack = <
({ value, maxCallCount }) => ({
args,
maxCallCount,
behavior: { type: 'return' as const, value },
behavior: { type: BehaviorType.RETURN, value },
calls: [],
}),
),
Expand All @@ -89,7 +97,7 @@ export const createBehaviorStack = <
({ value, maxCallCount }) => ({
args,
maxCallCount,
behavior: { type: 'resolve' as const, value },
behavior: { type: BehaviorType.RESOLVE, value },
calls: [],
}),
),
Expand All @@ -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: [],
}),
),
Expand All @@ -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: [],
}),
),
Expand All @@ -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: [],
}),
),
Expand Down
12 changes: 6 additions & 6 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}()`
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/stubs.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -24,28 +28,28 @@ export const configureStub = <TFunc extends AnyFunction>(

const implementation = (...args: Parameters<TFunc>): 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)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/vitest-when.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TFunc extends AnyFunction> {
Expand Down Expand Up @@ -49,9 +50,10 @@ export const debug = <TFunc extends AnyFunction>(
spy: TFunc,
options: DebugOptions = {},
): DebugResult => {
const log = options.log ?? true
const result = getDebug(spy)

if (options.log !== false) {
if (log) {
console.debug(result.description)
}

Expand Down

0 comments on commit e5194d6

Please sign in to comment.