Repository navigation
Expand file tree
/
Copy pathUnexpectedError.js
More file actions
367 lines (319 loc) · 9.94 KB
/
Copy pathUnexpectedError.js
File metadata and controls
367 lines (319 loc) · 9.94 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
const utils = require('./utils');
const defaultDepth = require('./defaultDepth');
const useFullStackTrace = require('./useFullStackTrace');
const errorMethodBlacklist = [
'message',
'line',
'sourceId',
'sourceURL',
'stack',
'stackArray',
].reduce((result, prop) => {
result[prop] = true;
return result;
}, {});
function UnexpectedError(expect, parent) {
this.errorMode = (expect && expect.errorMode) || 'default';
const base = Error.call(this, '');
if (Error.captureStackTrace) {
Error.captureStackTrace(this, UnexpectedError);
} else {
// Throw the error to make sure it has its stack serialized:
try {
throw base;
} catch (err) {}
this.stack = base.stack;
}
// Prevent flooding the screen in node-tap
// https://github.com/unexpectedjs/unexpected/issues/582
Object.defineProperties(this, {
expect: {
enumerable: false,
value: expect,
},
});
this.parent = parent || null;
this.name = 'UnexpectedError';
}
UnexpectedError.prototype = Object.create(Error.prototype);
UnexpectedError.prototype.useFullStackTrace = useFullStackTrace;
const missingOutputMessage =
'You must either provide a format or a magicpen instance';
UnexpectedError.prototype.outputFromOptions = function (options) {
if (!options) {
throw new Error(missingOutputMessage);
}
if (typeof options === 'string') {
return this.expect.createOutput(options);
}
if (options.isMagicPen) {
return options.clone();
}
if (options.output) {
return options.output.clone();
}
if (options.format) {
return this.expect.createOutput(options.format);
}
throw new Error(missingOutputMessage);
};
UnexpectedError.prototype._isUnexpected = true;
UnexpectedError.prototype.isUnexpected = true;
UnexpectedError.prototype.buildDiff = function (options) {
const output = this.outputFromOptions(options);
const expect = this.expect;
return (
this.createDiff &&
this.createDiff(
output,
(actual, expected) => expect.diff(actual, expected, output.clone()),
(v, depth) =>
output.clone().appendInspected(v, (depth || defaultDepth) - 1),
(actual, expected) => expect.equal(actual, expected)
)
);
};
UnexpectedError.prototype.getDefaultErrorMessage = function (options) {
const output = this.outputFromOptions(options);
if (this.expect.testDescription) {
output.append(this.expect.standardErrorMessage(output.clone(), options));
} else if (typeof this.output === 'function') {
this.output.call(output, output);
}
let errorWithDiff = this;
while (!errorWithDiff.createDiff && errorWithDiff.parent) {
errorWithDiff = errorWithDiff.parent;
}
if (errorWithDiff && errorWithDiff.createDiff) {
const comparison = errorWithDiff.buildDiff(options);
if (comparison) {
output.nl(2).append(comparison);
}
}
return output;
};
UnexpectedError.prototype.getNestedErrorMessage = function (options) {
const output = this.outputFromOptions(options);
if (this.expect.testDescription) {
output.append(this.expect.standardErrorMessage(output.clone(), options));
} else if (typeof this.output === 'function') {
this.output.call(output, output);
}
let parent = this.parent;
while (parent.getErrorMode() === 'bubble') {
parent = parent.parent;
}
if (typeof options === 'string') {
options = { format: options };
} else if (options && options.isMagicPen) {
options = { output: options };
}
output
.nl()
.indentLines()
.i()
.block(
parent.getErrorMessage(
utils.extend({}, options || {}, {
compact: this.expect.subject === parent.expect.subject,
})
)
);
return output;
};
UnexpectedError.prototype.getDefaultOrNestedMessage = function (options) {
if (this.hasDiff()) {
return this.getDefaultErrorMessage(options);
} else {
return this.getNestedErrorMessage(options);
}
};
UnexpectedError.prototype.hasDiff = function () {
return !!this.getDiffMethod();
};
UnexpectedError.prototype.getDiffMethod = function () {
let errorWithDiff = this;
while (!errorWithDiff.createDiff && errorWithDiff.parent) {
errorWithDiff = errorWithDiff.parent;
}
return (errorWithDiff && errorWithDiff.createDiff) || null;
};
UnexpectedError.prototype.getDiff = function (options) {
let errorWithDiff = this;
while (!errorWithDiff.createDiff && errorWithDiff.parent) {
errorWithDiff = errorWithDiff.parent;
}
return errorWithDiff && errorWithDiff.buildDiff(options);
};
UnexpectedError.prototype.getDiffMessage = function (options) {
const output = this.outputFromOptions(options);
const comparison = this.getDiff(options);
if (comparison) {
output.append(comparison);
} else if (this.expect.testDescription) {
output.append(this.expect.standardErrorMessage(output.clone(), options));
} else if (typeof this.output === 'function') {
this.output.call(output, output);
}
return output;
};
UnexpectedError.prototype.getErrorMode = function () {
if (!this.parent) {
switch (this.errorMode) {
case 'default':
case 'bubbleThrough':
return this.errorMode;
default:
return 'default';
}
} else {
return this.errorMode;
}
};
UnexpectedError.prototype.getErrorMessage = function (options) {
// Search for any parent error that has an error mode of 'bubbleThrough' through on the
// error these should be bubbled to the top
let errorWithBubbleThrough = this.parent;
while (
errorWithBubbleThrough &&
errorWithBubbleThrough.getErrorMode() !== 'bubbleThrough'
) {
errorWithBubbleThrough = errorWithBubbleThrough.parent;
}
if (errorWithBubbleThrough) {
return errorWithBubbleThrough.getErrorMessage(options);
}
const errorMode = this.getErrorMode();
switch (errorMode) {
case 'nested':
return this.getNestedErrorMessage(options);
case 'default':
return this.getDefaultErrorMessage(options);
case 'bubbleThrough':
return this.getDefaultErrorMessage(options);
case 'bubble':
return this.parent.getErrorMessage(options);
case 'diff':
return this.getDiffMessage(options);
case 'defaultOrNested':
return this.getDefaultOrNestedMessage(options);
default:
throw new Error(`Unknown error mode: '${errorMode}'`);
}
};
function findStackStart(lines) {
for (let i = lines.length - 1; i >= 0; i -= 1) {
if (lines[i] === '') {
return i + 1;
}
}
return -1;
}
UnexpectedError.prototype.serializeMessage = function (outputFormat) {
if (!this._hasSerializedErrorMessage) {
const htmlFormat = outputFormat === 'html';
if (htmlFormat) {
if (!('htmlMessage' in this)) {
this.htmlMessage = this.getErrorMessage({ format: 'html' }).toString();
}
}
this.message = `\n${this.getErrorMessage({
format: htmlFormat ? 'text' : outputFormat,
}).toString()}\n`;
if (
this.originalError &&
this.originalError instanceof Error &&
typeof this.originalError.stack === 'string'
) {
// The stack of the original error looks like this:
// <constructor name>: <error message>\n<actual stack trace>
// Try to get hold of <actual stack trace> and append it
// to the error message of this error:
const index = this.originalError.stack.indexOf(
this.originalError.message
);
if (index === -1) {
// Phantom.js doesn't include the error message in the stack property
this.stack = `${this.message}\n${this.originalError.stack}`;
} else {
this.stack =
this.message +
this.originalError.stack.substr(
index + this.originalError.message.length
);
}
} else if (/^(Unexpected)?Error:?\n/.test(this.stack)) {
// Fix for Jest that does not seem to capture the error message
const matchErrorName = /^(?:Unexpected)?Error:?\n/.exec(this.stack);
if (matchErrorName) {
this.stack = this.message + this.stack.substr(matchErrorName[0].length);
}
}
if (this.stack && !this.useFullStackTrace) {
const lines = this.stack.split(/\n/);
const stackStart = findStackStart(lines);
const newStack = lines.filter(
(line, i) =>
i < stackStart ||
(!/node_modules[/\\]unexpected(?:-[^/\\]+)?[/\\]/.test(line) &&
!/executeExpect.*node_modules[/\\]unexpected[/\\]/.test(
lines[i + 1]
))
);
if (newStack.length !== lines.length) {
const indentation = /^(\s*)/.exec(lines[lines.length - 1])[1];
if (outputFormat === 'html') {
newStack.push(
`${indentation}set the query parameter full-trace=true to see the full stack trace`
);
} else {
newStack.push(
`${indentation}set UNEXPECTED_FULL_TRACE=true to see the full stack trace`
);
}
}
this.stack = newStack.join('\n');
}
this._hasSerializedErrorMessage = true;
}
};
UnexpectedError.prototype.clone = function () {
const that = this;
const newError = new UnexpectedError(this.expect);
Object.keys(that).forEach((key) => {
if (!errorMethodBlacklist[key]) {
newError[key] = that[key];
}
});
return newError;
};
UnexpectedError.prototype.getLabel = function () {
let currentError = this;
while (currentError && !currentError.label) {
currentError = currentError.parent;
}
return (currentError && currentError.label) || null;
};
UnexpectedError.prototype.getParents = function () {
const result = [];
let parent = this.parent;
while (parent) {
result.push(parent);
parent = parent.parent;
}
return result;
};
UnexpectedError.prototype.getAllErrors = function () {
const result = this.getParents();
result.unshift(this);
return result;
};
if (Object.__defineGetter__) {
Object.defineProperty(UnexpectedError.prototype, 'htmlMessage', {
enumerable: true,
get() {
return this.getErrorMessage({ format: 'html' }).toString();
},
});
}
module.exports = UnexpectedError;