Skip to content

Commit e5194d6

Browse files
committed
fixup: ensure public types are exported
1 parent 6fd155f commit e5194d6

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -502,16 +502,16 @@ import type { DebugOptions } from 'vitest-when'
502502
#### `DebugResult`
503503

504504
```ts
505-
import type { DebugResult, DebugStubbing, DebugBehavior } from 'vitest-when'
505+
import type { DebugResult, Stubbing, Behavior } from 'vitest-when'
506506
```
507507

508508
| fields | type | description |
509509
| ---------------------------- | -------------------------------------------- | ----------------------------------------------------------- |
510510
| `description` | `string` | A human-readable description of the stub, logged by default |
511511
| `name` | `string` | The name of the mock, if set by [`mockName`][mockName] |
512-
| `stubbings` | `DebugStubbing[]` | The list of configured stub behaviors |
512+
| `stubbings` | `Stubbing[]` | The list of configured stub behaviors |
513513
| `stubbings[].args` | `unknown[]` | The stubbing's arguments to match |
514-
| `stubbings[].behavior` | `DebugBehavior` | The configured behavior of the stubbing |
514+
| `stubbings[].behavior` | `Behavior` | The configured behavior of the stubbing |
515515
| `stubbings[].behavior.type` | `return`, `throw`, `resolve`, `reject`, `do` | Result type of the stubbing |
516516
| `stubbings[].behavior.value` | `unknown` | Value for the behavior, if `type` is `return` or `resolve` |
517517
| `stubbings[].behavior.error` | `unknown` | Error for the behavior, it `type` is `throw` or `reject` |

src/behaviors.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ export interface BehaviorEntry<TArgs extends unknown[]> {
3333
maxCallCount?: number | undefined
3434
}
3535

36+
export const BehaviorType = {
37+
RETURN: 'return',
38+
RESOLVE: 'resolve',
39+
THROW: 'throw',
40+
REJECT: 'reject',
41+
DO: 'do',
42+
} as const
43+
3644
export type Behavior =
37-
| { type: 'return'; value: unknown }
38-
| { type: 'resolve'; value: unknown }
39-
| { type: 'throw'; error: unknown }
40-
| { type: 'reject'; error: unknown }
41-
| { type: 'do'; callback: AnyFunction }
45+
| { type: typeof BehaviorType.RETURN; value: unknown }
46+
| { type: typeof BehaviorType.RESOLVE; value: unknown }
47+
| { type: typeof BehaviorType.THROW; error: unknown }
48+
| { type: typeof BehaviorType.REJECT; error: unknown }
49+
| { type: typeof BehaviorType.DO; callback: AnyFunction }
4250

4351
export interface BehaviorOptions<TValue> {
4452
value: TValue
@@ -77,7 +85,7 @@ export const createBehaviorStack = <
7785
({ value, maxCallCount }) => ({
7886
args,
7987
maxCallCount,
80-
behavior: { type: 'return' as const, value },
88+
behavior: { type: BehaviorType.RETURN, value },
8189
calls: [],
8290
}),
8391
),
@@ -89,7 +97,7 @@ export const createBehaviorStack = <
8997
({ value, maxCallCount }) => ({
9098
args,
9199
maxCallCount,
92-
behavior: { type: 'resolve' as const, value },
100+
behavior: { type: BehaviorType.RESOLVE, value },
93101
calls: [],
94102
}),
95103
),
@@ -101,7 +109,7 @@ export const createBehaviorStack = <
101109
({ value, maxCallCount }) => ({
102110
args,
103111
maxCallCount,
104-
behavior: { type: 'throw' as const, error: value },
112+
behavior: { type: BehaviorType.THROW, error: value },
105113
calls: [],
106114
}),
107115
),
@@ -113,7 +121,7 @@ export const createBehaviorStack = <
113121
({ value, maxCallCount }) => ({
114122
args,
115123
maxCallCount,
116-
behavior: { type: 'reject' as const, error: value },
124+
behavior: { type: BehaviorType.REJECT, error: value },
117125
calls: [],
118126
}),
119127
),
@@ -125,7 +133,7 @@ export const createBehaviorStack = <
125133
({ value, maxCallCount }) => ({
126134
args,
127135
maxCallCount,
128-
behavior: { type: 'do' as const, callback: value },
136+
behavior: { type: BehaviorType.DO, callback: value },
129137
calls: [],
130138
}),
131139
),

src/debug.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55

66
import { validateSpy, getBehaviorStack } from './stubs'
77
import type { AnyFunction } from './types'
8-
import type { Behavior } from './behaviors'
8+
import { type Behavior, BehaviorType } from './behaviors'
99

1010
export interface DebugResult {
1111
name: string
@@ -71,23 +71,23 @@ const formatCall = (args: readonly unknown[]): string => {
7171

7272
const formatBehavior = (behavior: Behavior): string => {
7373
switch (behavior.type) {
74-
case 'return': {
74+
case BehaviorType.RETURN: {
7575
return `=> ${stringify(behavior.value)}`
7676
}
7777

78-
case 'resolve': {
78+
case BehaviorType.RESOLVE: {
7979
return `=> Promise.resolve(${stringify(behavior.value)})`
8080
}
8181

82-
case 'throw': {
82+
case BehaviorType.THROW: {
8383
return `=> { throw ${stringify(behavior.error)} }`
8484
}
8585

86-
case 'reject': {
86+
case BehaviorType.REJECT: {
8787
return `=> Promise.reject(${stringify(behavior.error)})`
8888
}
8989

90-
case 'do': {
90+
case BehaviorType.DO: {
9191
return `=> ${stringify(behavior.callback)}()`
9292
}
9393
}

src/stubs.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { type Mock as Spy } from 'vitest'
2-
import { createBehaviorStack, type BehaviorStack } from './behaviors.ts'
2+
import {
3+
createBehaviorStack,
4+
type BehaviorStack,
5+
BehaviorType,
6+
} from './behaviors.ts'
37
import { NotAMockFunctionError } from './errors.ts'
48
import type { AnyFunction } from './types.ts'
59

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

2529
const implementation = (...args: Parameters<TFunc>): unknown => {
2630
const behavior = behaviors.use(args)?.behavior ?? {
27-
type: 'return',
31+
type: BehaviorType.RETURN,
2832
value: undefined,
2933
}
3034

3135
switch (behavior.type) {
32-
case 'return': {
36+
case BehaviorType.RETURN: {
3337
return behavior.value
3438
}
3539

36-
case 'resolve': {
40+
case BehaviorType.RESOLVE: {
3741
return Promise.resolve(behavior.value)
3842
}
3943

40-
case 'throw': {
44+
case BehaviorType.THROW: {
4145
throw behavior.error
4246
}
4347

44-
case 'reject': {
48+
case BehaviorType.REJECT: {
4549
return Promise.reject(behavior.error)
4650
}
4751

48-
case 'do': {
52+
case BehaviorType.DO: {
4953
return behavior.callback(...args)
5054
}
5155
}

src/vitest-when.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { WhenOptions } from './behaviors.ts'
33
import type { AnyFunction } from './types.ts'
44
import { getDebug, type DebugResult } from './debug.ts'
55

6-
export type { WhenOptions } from './behaviors.ts'
6+
export { type WhenOptions, type Behavior, BehaviorType } from './behaviors.ts'
7+
export type { DebugResult, Stubbing } from './debug.ts'
78
export * from './errors.ts'
89

910
export interface StubWrapper<TFunc extends AnyFunction> {
@@ -49,9 +50,10 @@ export const debug = <TFunc extends AnyFunction>(
4950
spy: TFunc,
5051
options: DebugOptions = {},
5152
): DebugResult => {
53+
const log = options.log ?? true
5254
const result = getDebug(spy)
5355

54-
if (options.log !== false) {
56+
if (log) {
5557
console.debug(result.description)
5658
}
5759

0 commit comments

Comments
 (0)