-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathjest-config.ts
149 lines (132 loc) · 3.83 KB
/
jest-config.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
// eslint-disable-next-line import/no-extraneous-dependencies
import { CustomConsole, LogType, LogMessage } from '@jest/console';
const RESET = '\u001b[0;0m';
const GREY = '\u001b[30;1m';
// const GREY = '\u001b[90;1m'; // BRIGHT BLACK
const RED = '\u001b[31;1m';
const CYAN = '\u001b[36;1m';
const REVERSED_RED = '\u001b[91;7m';
const BLUE = '\u001b[34;1m';
// const BRIGHT_BLUE = '\u001b[94;1m';
const YELLOW = '\u001b[33;0m';
// const DIM_YELLOW = '\u001b[33;2m';
// const GREY_BG = '\u001b[40;1m';
function simpleFormatter(type: LogType, ...message: LogMessage[]): string {
const CONSOLE_INDENT = ' ';
let s = message.map((x) => x.toString()).join('');
if (type === 'assert')
s =
'\n' +
REVERSED_RED +
' ASSERTION FAILURE ' +
RESET +
RED +
' ' +
message +
RESET;
else if (type === 'error')
s = REVERSED_RED + ' ERROR ' + RESET + RED + ' ' + message + RESET;
else if (type === 'log') s = BLUE + message + RESET;
else if (type === 'warn') s = YELLOW + 'WARNING ' + YELLOW + message + RESET;
return s
.split(/\n/)
.map((line) => CONSOLE_INDENT + line)
.join('\n');
}
function formatStackTrace(s: string): string {
const curDir = __dirname;
const parentDir = curDir.match(/(.*)\/([^\/]*)$/)?.[1];
if (!parentDir || !curDir) return s;
s = s
.replace(new RegExp(parentDir, 'g'), '..')
.replace(new RegExp(curDir, 'g'), '.');
const lines = s.split('\n').map((line) => {
if (/\.\.\/node_modules/.test(line)) return '';
if (/\(node:internal/.test(line)) return '';
line = line.replace(' at ', ' > ');
const [_, prefix, filename, suffix] =
line.match(/(?:(.+)\()([^:]+)(\:[^)]+)/) ?? [];
if (!prefix) return line;
if (/Object.<anonymous>/.test(prefix) || prefix.startsWith(' > console.'))
return GREY + ' > ' + CYAN + filename + GREY + suffix;
return GREY + prefix + CYAN + filename + GREY + suffix;
});
lines[0] = '';
lines[1] = '';
return RESET + lines.filter((line) => line.length > 0).join('\n') + RESET;
}
/**
* @noInheritDoc
*/
class CortexConsole extends CustomConsole {
constructor(stdout, stderr, formatBuffer = (_type, message) => message) {
super(stdout, stderr, formatBuffer);
}
log(...message) {
let msg = '';
try {
throw new Error();
} catch (e) {
msg = `${
!message ? '' : message.map((x) => serialize(x)).join(' ')
}\n${formatStackTrace(e.stack)}\n`;
}
this['_logError']('log', msg);
}
error(...message) {
debugger;
let msg = '';
try {
throw new Error();
} catch (e) {
msg = `${
!message ? '' : message.map((x) => serialize(x)).join(' ')
}\n${formatStackTrace(e.stack)}\n`;
}
this['_logError']('error', msg);
}
assert(value, ...message) {
if (value) return;
debugger;
let msg = '';
try {
throw new Error();
} catch (e) {
msg = `${
!message ? '' : message.map((x) => serialize(x)).join(' ')
}\n${formatStackTrace(e.stack)}\n`;
}
this['_logError']('assert', msg);
}
}
function recursiveSerialize(x: unknown): string {
if (x === null) return 'null';
if (
typeof x === 'object' &&
!Array.isArray(x) &&
x.constructor.name !== 'Object'
)
return `{${x.constructor.name}}`;
return serialize(x);
}
function serialize(x: any): string {
if (Array.isArray(x)) {
return `[${x.map((y) => recursiveSerialize(y)).join(', ')}]`;
}
if (x === null) return 'null';
if (x === undefined) return 'undefined';
if (typeof x === 'object') {
return `[${Object.entries(x)
.map(([key, value]) => '"' + key + '": ' + recursiveSerialize(value))
.join(', ')}]`;
}
if (typeof x === 'string') {
return `"${x}"`;
}
return x.toString();
}
global.console = new CortexConsole(
process.stdout,
process.stderr,
simpleFormatter
) as Console;