This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
/
diag.ts
192 lines (157 loc) · 4.65 KB
/
diag.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
import * as fs from 'fs';
import * as path from 'path';
import cloneDeep = require('lodash.clonedeep');
import type { ExceptionDiag, Result } from 'tap-parser';
import { formatStackTrace } from 'jest-message-util';
import { printDiffOrStringify } from 'jest-matcher-utils';
import { codeFrameColumns } from '@babel/code-frame';
import chalk = require('chalk');
import { Context } from '../context';
import * as yaml from 'yaml';
export function renderDiag(context: Context, origFailure: Result): string {
const failure = cloneDeep(origFailure);
let msg = '';
if ('returnedPromiseRejection' === failure?.diag?.tapCaught) {
msg += renderException(context, failure as Result<ExceptionDiag>);
delete failure.diag.tapCaught;
} else if (failure?.diag) {
const diag: any = failure?.diag;
if ('found' in diag && 'wanted' in diag && 'compare' in diag) {
const diff = printDiffOrStringify(
diag.wanted,
diag.found,
' found',
' wanted',
true,
);
msg += indent(`${diff}\ncompare: ${diag.compare}\n\n`);
delete diag.found;
delete diag.wanted;
delete diag.compare;
delete failure.name;
delete failure.fullname;
}
if ('diff' in diag) {
msg += indent(highlightDiff(diag.diff), ' ');
delete diag.diff;
delete failure.name;
delete failure.fullname;
}
if (diag.stack) {
msg += indent('The failure occurred here:\n');
msg += repairStackTrace(context, diag.stack);
msg += '\n\n';
maybeDeleteStackInfo(msg, diag);
}
if (diag.origin?.stack) {
msg += indent('The error was:\n');
msg += repairStackTrace(context, diag.origin.stack);
maybeDeleteStackInfo(msg, diag.origin);
if (isEmpty(diag.origin)) {
delete diag.origin;
}
if (isEmpty(diag.found)) {
delete diag.origin;
}
// no idea what this is (t.throws)
if (isEmpty(diag.found)) {
delete diag.found;
}
}
if (diag.at) {
try {
msg += indent(
`Extra location information:\n\n${renderAtWithoutStack(
context,
diag,
)}\n\n`,
);
} catch (e) {
// just leave the `at` as is
}
}
}
delete failure.id;
delete failure.ok;
if (isEmpty(failure.diag)) {
delete failure.diag;
}
if (msg) {
return `${msg}\n${fallback(failure)}`;
}
return fallback(failure);
}
function fallback(failure: Partial<Result>): string {
if (isEmpty(failure)) {
return '';
}
let msg = `Additionally, ${chalk.grey('tap')} said:\n\n`;
msg += yaml.stringify(failure);
return indent(msg);
}
function repairStackTrace(context: Context, stack: string): string {
const ruined = stack
.trimRight()
.replace(/\((?!\/)/g, `(${context.globalConfig.rootDir}/`)
.replace(/^/gm, ' at ');
return formatStackTrace(ruined, context.projectConfig, context.globalConfig);
}
function renderAtWithoutStack(context: Context, diag: any): string {
const file = path.join(context.globalConfig.rootDir, diag.at.file);
const msg = codeFrameColumns(
fs.readFileSync(file).toString('utf-8'),
{ start: diag.at },
{ highlightCode: true },
);
delete diag.at;
delete diag.source;
return msg;
}
function maybeDeleteStackInfo(rendered: string, diag: any): void {
delete diag.stack;
// guessing if it has line-numbered sections; terrible
if (-1 !== rendered.indexOf(' | ')) {
delete diag.source;
delete diag.at;
}
}
// I can't believe there isn't a convenient version of this lying around
function highlightDiff(diff: string): string {
let out = '';
for (const line of diff.split('\n')) {
out += diffColour(line[0])(line) + '\n';
}
return out;
}
function diffColour(char: string): (value: string) => string {
switch (char) {
case '+':
return chalk.green;
case '-':
return chalk.red;
case '@':
return chalk.yellow;
default:
return (s: string): string => s;
}
}
function renderException(
context: Context,
failure: Result<ExceptionDiag>,
): string {
// undo tap's pretty formatting, and let jest have at it instead
const stackAndMaybeCode = repairStackTrace(context, failure.diag.stack);
const msg = ` ${failure.diag.type}: ${failure.name}\n${stackAndMaybeCode}`;
delete failure.name;
delete failure.fullname;
delete failure.diag.type;
delete failure.diag.test;
maybeDeleteStackInfo(stackAndMaybeCode, failure.diag);
return msg;
}
function indent(val: string, by = ' '): string {
return val.replace(/^(?!\s*$)/gm, by);
}
function isEmpty(val: object | undefined): val is {} {
return !val || 0 === Object.keys(val).length;
}