-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebug.ts
152 lines (130 loc) · 3.84 KB
/
debug.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
format as prettyFormat,
plugins as prettyFormatPlugins,
} from 'pretty-format'
import { validateSpy, getBehaviorStack } from './stubs'
import type { AnyFunction, MockInstance } from './types'
import { type Behavior, BehaviorType } from './behaviors'
export interface DebugResult {
name: string
description: string
stubbings: readonly Stubbing[]
unmatchedCalls: readonly unknown[][]
}
export interface Stubbing {
args: readonly unknown[]
behavior: Behavior
calls: readonly unknown[][]
}
export const getDebug = <TFunc extends AnyFunction>(
spy: TFunc | MockInstance<TFunc>,
): DebugResult => {
const target = validateSpy<TFunc>(spy)
const name = target.getMockName()
const behaviors = getBehaviorStack(target)
const unmatchedCalls = behaviors?.getUnmatchedCalls() ?? target.mock.calls
const stubbings =
behaviors?.getAll().map((entry) => ({
args: entry.args,
behavior: entry.behavior,
calls: entry.calls,
})) ?? []
const result = { name, stubbings, unmatchedCalls }
const description = formatDebug(result)
return { ...result, description }
}
const formatDebug = (debug: Omit<DebugResult, 'description'>): string => {
const { name, stubbings, unmatchedCalls } = debug
const callCount = stubbings.reduce(
(result, { calls }) => result + calls.length,
0,
)
const stubbingCount = stubbings.length
const unmatchedCallsCount = unmatchedCalls.length
return [
`\`${name}()\` has:`,
`* ${count(stubbingCount, 'stubbing')} with ${count(callCount, 'call')}`,
...stubbings.map((stubbing) => ` * ${formatStubbing(stubbing)}`).reverse(),
`* ${count(unmatchedCallsCount, 'unmatched call')}`,
...unmatchedCalls.map((args) => ` * \`${formatCall(args)}\``),
'',
].join('\n')
}
const formatStubbing = ({ args, behavior, calls }: Stubbing): string => {
return `Called ${count(calls.length, 'time')}: \`${formatCall(
args,
)} ${formatBehavior(behavior)}\``
}
const formatCall = (args: readonly unknown[]): string => {
return `(${args.map((a) => stringify(a)).join(', ')})`
}
const formatBehavior = (behavior: Behavior): string => {
switch (behavior.type) {
case BehaviorType.RETURN: {
return `=> ${stringify(behavior.value)}`
}
case BehaviorType.RESOLVE: {
return `=> Promise.resolve(${stringify(behavior.value)})`
}
case BehaviorType.THROW: {
return `=> { throw ${stringify(behavior.error)} }`
}
case BehaviorType.REJECT: {
return `=> Promise.reject(${stringify(behavior.error)})`
}
case BehaviorType.DO: {
return `=> ${stringify(behavior.callback)}()`
}
}
}
const count = (amount: number, thing: string) =>
`${amount} ${thing}${amount === 1 ? '' : 's'}`
const {
AsymmetricMatcher,
DOMCollection,
DOMElement,
Immutable,
ReactElement,
ReactTestComponent,
} = prettyFormatPlugins
const FORMAT_PLUGINS = [
ReactTestComponent,
ReactElement,
DOMElement,
DOMCollection,
Immutable,
AsymmetricMatcher,
]
const FORMAT_MAX_LENGTH = 10_000
/**
* Stringify a value.
*
* Copied from `jest-matcher-utils`
* https://github.com/jestjs/jest/blob/654dbd6f6b3d94c604221e1afd70fcfb66f9478e/packages/jest-matcher-utils/src/index.ts#L96
*/
const stringify = (object: unknown, maxDepth = 10, maxWidth = 10): string => {
let result
try {
result = prettyFormat(object, {
maxDepth,
maxWidth,
min: true,
plugins: FORMAT_PLUGINS,
})
} catch {
result = prettyFormat(object, {
callToJSON: false,
maxDepth,
maxWidth,
min: true,
plugins: FORMAT_PLUGINS,
})
}
if (result.length >= FORMAT_MAX_LENGTH && maxDepth > 1) {
return stringify(object, Math.floor(maxDepth / 2), maxWidth)
} else if (result.length >= FORMAT_MAX_LENGTH && maxWidth > 1) {
return stringify(object, maxDepth, Math.floor(maxWidth / 2))
} else {
return result
}
}