-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
result-to-error.ts
68 lines (63 loc) · 1.63 KB
/
result-to-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
// turn a tap-parser Result object into an Error
// Put some of the diagnostic info into the error message,
// because that is the string the node --test reporter shows.
import { Base } from '@tapjs/core'
import {
CallSiteLike,
CallSiteLikeJSON,
expandStack,
} from '@tapjs/stack'
import { Result, TapError } from 'tap-parser'
import { locFromCallSite } from './loc-from-callsite.js'
import { prettyDiff } from './pretty-diff.js'
export const resultToError = (
result:
| Result
| TapError
| {
ok?: boolean
tapError?: string
name?: string
diag?: Result['diag']
},
test?: Base,
): Error => {
const {
stack: diagStack,
diff: rawDiff,
source,
passes: _,
...diag
} = result.diag || {}
const at: CallSiteLike | CallSiteLikeJSON =
diag.at ?? test?.options.at
const lfa = !!at ? locFromCallSite(at) : undefined
const stack =
diagStack ||
(lfa?.file ? `${lfa.file}:${lfa.line}:${lfa.column}` : undefined)
delete diag.at
const diff = prettyDiff(rawDiff)
if (diff) {
delete diag.actual
delete diag.expected
delete diag.found
delete diag.wanted
} else if (result.diag) {
if ('found' in diag) {
diag.actual = diag.found
}
if ('wanted' in diag) {
diag.expected = diag.wanted
}
delete diag.found
delete diag.wanted
}
const name =
(result.name || result.tapError || 'unnamed test') +
(diff ? '\n' + diff : '') +
(source ? '\n| ' + source.trimEnd().split('\n').join('\n| ') : '')
return Object.assign(new Error(name), {
...diag,
stack: `Error: ${name}\n` + expandStack(stack).trimEnd(),
})
}