-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
error.ts
235 lines (198 loc) · 6.67 KB
/
error.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* eslint-disable prefer-template */
import { existsSync, readFileSync } from 'fs'
import { join, normalize, relative } from 'pathe'
import c from 'picocolors'
import cliTruncate from 'cli-truncate'
import type { ErrorWithDiff, ParsedStack, Position } from '../types'
import { interpretSourcePos, lineSplitRE, parseStacktrace, posToNumber } from '../utils/source-map'
import { F_POINTER } from '../utils/figures'
import { stringify } from '../integrations/chai/jest-matcher-utils'
import type { Vitest } from './core'
import { unifiedDiff } from './diff'
import { divider } from './reporters/renderers/utils'
export function fileFromParsedStack(stack: ParsedStack) {
if (stack?.sourcePos?.source?.startsWith('..'))
return join(stack.file, '../', stack.sourcePos.source)
return stack.file
}
interface PrintErrorOptions {
type?: string
fullStack?: boolean
showCodeFrame?: boolean
}
export async function printError(error: unknown, ctx: Vitest, options: PrintErrorOptions = {}) {
const { showCodeFrame = true, fullStack = false, type } = options
let e = error as ErrorWithDiff
if (typeof error === 'string') {
e = {
message: error.split(/\n/g)[0],
stack: error,
} as any
}
const stacks = parseStacktrace(e, fullStack)
await interpretSourcePos(stacks, ctx)
const nearest = stacks.find(stack =>
ctx.server.moduleGraph.getModuleById(stack.file)
&& existsSync(stack.file),
)
const errorProperties = getErrorProperties(e)
if (type)
printErrorType(type, ctx)
printErrorMessage(e, ctx.console)
printStack(ctx, stacks, nearest, errorProperties, (s, pos) => {
if (showCodeFrame && s === nearest && nearest) {
const sourceCode = readFileSync(fileFromParsedStack(nearest), 'utf-8')
ctx.log(c.yellow(generateCodeFrame(sourceCode, 4, pos)))
}
})
if (e.cause) {
e.cause.name = `Caused by: ${e.cause.name}`
await printError(e.cause, ctx, { fullStack, showCodeFrame: false })
}
handleImportOutsideModuleError(e.stack || e.stackStr || '', ctx)
if (e.showDiff)
displayDiff(stringify(e.actual), stringify(e.expected), ctx.console, ctx.config.outputTruncateLength)
}
function printErrorType(type: string, ctx: Vitest) {
ctx.error(`\n${c.red(divider(c.bold(c.inverse(` ${type} `))))}`)
}
const skipErrorProperties = [
'message',
'name',
'nameStr',
'stack',
'cause',
'stacks',
'stackStr',
'type',
'showDiff',
'actual',
'expected',
'constructor',
'toString',
]
function getErrorProperties(e: ErrorWithDiff) {
const errorObject = Object.create(null)
if (e.name === 'AssertionError')
return errorObject
for (const key of Object.getOwnPropertyNames(e)) {
if (!skipErrorProperties.includes(key))
errorObject[key] = e[key as keyof ErrorWithDiff]
}
return errorObject
}
const esmErrors = [
'Cannot use import statement outside a module',
'Unexpected token \'export\'',
]
function handleImportOutsideModuleError(stack: string, ctx: Vitest) {
if (!esmErrors.some(e => stack.includes(e)))
return
const path = normalize(stack.split('\n')[0].trim())
let name = path.split('/node_modules/').pop() || ''
if (name?.startsWith('@'))
name = name.split('/').slice(0, 2).join('/')
else
name = name.split('/')[0]
ctx.error(c.yellow(
`Module ${path} seems to be an ES Module but shipped in a CommonJS package. `
+ `You might want to create an issue to the package ${c.bold(`"${name}"`)} asking `
+ 'them to ship the file in .mjs extension or add "type": "module" in their package.json.'
+ '\n\n'
+ 'As a temporary workaround you can try to inline the package by updating your config:'
+ '\n\n'
+ c.gray(c.dim('// vitest.config.js'))
+ '\n'
+ c.green(`export default {
test: {
deps: {
inline: [
${c.yellow(c.bold(`"${name}"`))}
]
}
}
}\n`)))
}
function displayDiff(actual: string, expected: string, console: Console, outputTruncateLength?: number) {
console.error(c.gray(unifiedDiff(actual, expected, { outputTruncateLength })) + '\n')
}
function printErrorMessage(error: ErrorWithDiff, console: Console) {
const errorName = error.name || error.nameStr || 'Unknown Error'
console.error(c.red(`${c.bold(errorName)}: ${error.message}`))
}
function printStack(
ctx: Vitest,
stack: ParsedStack[],
highlight: ParsedStack | undefined,
errorProperties: Record<string, unknown>,
onStack?: ((stack: ParsedStack, pos: Position) => void),
) {
if (!stack.length)
return
for (const frame of stack) {
const pos = frame.sourcePos || frame
const color = frame === highlight ? c.yellow : c.gray
const file = fileFromParsedStack(frame)
const path = relative(ctx.config.root, file)
ctx.log(color(` ${c.dim(F_POINTER)} ${[frame.method, c.dim(`${path}:${pos.line}:${pos.column}`)].filter(Boolean).join(' ')}`))
onStack?.(frame, pos)
// reached at test file, skip the follow stack
if (frame.file in ctx.state.filesMap)
break
}
ctx.log()
const hasProperties = Object.keys(errorProperties).length > 0
if (hasProperties) {
ctx.log(c.red(c.dim(divider())))
const propertiesString = stringify(errorProperties, 10, { printBasicPrototype: false })
ctx.log(c.red(c.bold('Serialized Error:')), c.gray(propertiesString))
}
}
export function generateCodeFrame(
source: string,
indent = 0,
start: number | Position = 0,
end?: number,
range = 2,
): string {
start = posToNumber(source, start)
end = end || start
const lines = source.split(lineSplitRE)
let count = 0
let res: string[] = []
const columns = process.stdout?.columns || 80
function lineNo(no: number | string = '') {
return c.gray(`${String(no).padStart(3, ' ')}| `)
}
for (let i = 0; i < lines.length; i++) {
count += lines[i].length + 1
if (count >= start) {
for (let j = i - range; j <= i + range || end > count; j++) {
if (j < 0 || j >= lines.length)
continue
const lineLength = lines[j].length
// to long, maybe it's a minified file, skip for codeframe
if (lineLength > 200)
return ''
res.push(lineNo(j + 1) + cliTruncate(lines[j], columns - 5 - indent))
if (j === i) {
// push underline
const pad = start - (count - lineLength)
const length = Math.max(1, end > count ? lineLength - pad : end - start)
res.push(lineNo() + ' '.repeat(pad) + c.red('^'.repeat(length)))
}
else if (j > i) {
if (end > count) {
const length = Math.max(1, Math.min(end - count, lineLength))
res.push(lineNo() + c.red('^'.repeat(length)))
}
count += lineLength + 1
}
}
break
}
}
if (indent)
res = res.map(line => ' '.repeat(indent) + line)
return res.join('\n')
}