forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
267 lines (228 loc) Β· 7.35 KB
/
index.js
File metadata and controls
267 lines (228 loc) Β· 7.35 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Glob, Path} from 'types/Config';
import type {AssertionResult, TestResult} from 'types/TestResult';
import path from 'path';
import chalk from 'chalk';
import micromatch from 'micromatch';
import slash from 'slash';
import StackUtils from 'stack-utils';
let nodeInternals = [];
try {
nodeInternals = StackUtils.nodeInternals()
// Somehow we get a trace without the `process._tickCallback` part
.concat(new RegExp('internal/process/next_tick.js'));
} catch (e) {
// `StackUtils.nodeInternals()` fails in browsers. We don't need to remove
// node internals in the browser though, so no issue.
}
type StackTraceConfig = {
rootDir: string,
testMatch: Array<Glob>,
};
type StackTraceOptions = {
noStackTrace: boolean,
};
// filter for noisy stack trace lines
const JASMINE_IGNORE = /^\s+at(?:(?:.*?vendor\/|jasmine\-)|\s+jasmine\.buildExpectationResult)/;
const JEST_INTERNALS_IGNORE = /^\s+at.*?jest(-.*?)?(\/|\\)(build|node_modules|packages)(\/|\\)/;
const ANONYMOUS_FN_IGNORE = /^\s+at <anonymous>.*$/;
const ANONYMOUS_PROMISE_IGNORE = /^\s+at (new )?Promise \(<anonymous>\).*$/;
const ANONYMOUS_GENERATOR_IGNORE = /^\s+at Generator.next \(<anonymous>\).*$/;
const TITLE_INDENT = ' ';
const MESSAGE_INDENT = ' ';
const STACK_INDENT = ' ';
const ANCESTRY_SEPARATOR = ' \u203A ';
const TITLE_BULLET = chalk.bold('\u25cf ');
const STACK_TRACE_COLOR = chalk.dim;
const STACK_PATH_REGEXP = /\s*at.*\(?(\:\d*\:\d*|native)\)?/;
const EXEC_ERROR_MESSAGE = 'Test suite failed to run';
const ERROR_TEXT = 'Error: ';
const trim = string => (string || '').replace(/^\s+/, '').replace(/\s+$/, '');
// Some errors contain not only line numbers in stack traces
// e.g. SyntaxErrors can contain snippets of code, and we don't
// want to trim those, because they may have pointers to the column/character
// which will get misaligned.
const trimPaths = string =>
string.match(STACK_PATH_REGEXP) ? trim(string) : string;
// ExecError is an error thrown outside of the test suite (not inside an `it` or
// `before/after each` hooks). If it's thrown, none of the tests in the file
// are executed.
export const formatExecError = (
testResult: TestResult,
config: StackTraceConfig,
options: StackTraceOptions,
testPath: Path,
) => {
let error = testResult.testExecError;
if (!error || typeof error === 'number') {
error = new Error(`Expected an Error, but "${String(error)}" was thrown`);
error.stack = '';
}
let {message, stack} = error;
if (typeof error === 'string' || !error) {
error || (error = 'EMPTY ERROR');
message = '';
stack = error;
}
const separated = separateMessageFromStack(stack || '');
stack = separated.stack;
if (separated.message.indexOf(trim(message)) !== -1) {
// Often stack trace already contains the duplicate of the message
message = separated.message;
}
message = message
.split(/\n/)
.map(line => MESSAGE_INDENT + line)
.join('\n');
stack =
stack && !options.noStackTrace
? '\n' + formatStackTrace(stack, config, options, testPath)
: '';
if (message.match(/^\s*$/) && stack.match(/^\s*$/)) {
// this can happen if an empty object is thrown.
message = MESSAGE_INDENT + 'Error: No message was provided';
}
return (
TITLE_INDENT +
TITLE_BULLET +
EXEC_ERROR_MESSAGE +
'\n\n' +
message +
stack +
'\n'
);
};
const removeInternalStackEntries = (lines, options: StackTraceOptions) => {
let pathCounter = 0;
return lines.filter(line => {
if (ANONYMOUS_FN_IGNORE.test(line)) {
return false;
}
if (ANONYMOUS_PROMISE_IGNORE.test(line)) {
return false;
}
if (ANONYMOUS_GENERATOR_IGNORE.test(line)) {
return false;
}
if (nodeInternals.some(internal => internal.test(line))) {
return false;
}
if (!STACK_PATH_REGEXP.test(line)) {
return true;
}
if (JASMINE_IGNORE.test(line)) {
return false;
}
if (++pathCounter === 1) {
return true; // always keep the first line even if it's from Jest
}
if (options.noStackTrace) {
return false;
}
if (JEST_INTERNALS_IGNORE.test(line)) {
return false;
}
return true;
});
};
const formatPaths = (
config: StackTraceConfig,
options: StackTraceOptions,
relativeTestPath,
line,
) => {
// Extract the file path from the trace line.
const match = line.match(/(^\s*at .*?\(?)([^()]+)(:[0-9]+:[0-9]+\)?.*$)/);
if (!match) {
return line;
}
let filePath = slash(path.relative(config.rootDir, match[2]));
// highlight paths from the current test file
if (
(config.testMatch &&
config.testMatch.length &&
micromatch(filePath, config.testMatch)) ||
filePath === relativeTestPath
) {
filePath = chalk.reset.cyan(filePath);
}
return STACK_TRACE_COLOR(match[1]) + filePath + STACK_TRACE_COLOR(match[3]);
};
export const formatStackTrace = (
stack: string,
config: StackTraceConfig,
options: StackTraceOptions,
testPath: ?Path,
) => {
let lines = stack.split(/\n/);
const relativeTestPath = testPath
? slash(path.relative(config.rootDir, testPath))
: null;
lines = removeInternalStackEntries(lines, options);
return lines
.map(trimPaths)
.map(formatPaths.bind(null, config, options, relativeTestPath))
.map(line => STACK_INDENT + line)
.join('\n');
};
export const formatResultsErrors = (
testResults: Array<AssertionResult>,
config: StackTraceConfig,
options: StackTraceOptions,
testPath: ?Path,
): ?string => {
const failedResults = testResults.reduce((errors, result) => {
result.failureMessages.forEach(content => errors.push({content, result}));
return errors;
}, []);
if (!failedResults.length) {
return null;
}
return failedResults
.map(({result, content}) => {
let {message, stack} = separateMessageFromStack(content);
stack = options.noStackTrace
? ''
: STACK_TRACE_COLOR(
formatStackTrace(stack, config, options, testPath),
) + '\n';
message = message
.split(/\n/)
.map(line => MESSAGE_INDENT + line)
.join('\n');
const title =
chalk.bold.red(
TITLE_INDENT +
TITLE_BULLET +
result.ancestorTitles.join(ANCESTRY_SEPARATOR) +
(result.ancestorTitles.length ? ANCESTRY_SEPARATOR : '') +
result.title,
) + '\n';
return title + '\n' + message + '\n' + stack;
})
.join('\n');
};
// jasmine and worker farm sometimes don't give us access to the actual
// Error object, so we have to regexp out the message from the stack string
// to format it.
export const separateMessageFromStack = (content: string) => {
if (!content) {
return {message: '', stack: ''};
}
const messageMatch = content.match(/(^(.|\n)*?(?=\n\s*at\s.*\:\d*\:\d*))/);
let message = messageMatch ? messageMatch[0] : 'Error';
const stack = messageMatch ? content.slice(message.length) : content;
// If the error is a plain error instead of a SyntaxError or TypeError
// we remove it from the message because it is generally not useful.
if (message.startsWith(ERROR_TEXT)) {
message = message.substr(ERROR_TEXT.length);
}
return {message, stack};
};