-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
415 lines (386 loc) · 11.1 KB
/
client.js
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*globals Mocha, mocha, window, document, Attr, Comment, Element,
DocumentFragment, HTMLCollection, NamedNodeMap, NodeList, Text, ShadowRoot */
'use strict';
const constants = Mocha.Runner.constants;
const MAX_DEPTH = 5;
const { propertyIsEnumerable } = Object.prototype;
const { filter, forEach, map, push, pop, shift, slice } = Array.prototype;
const queue = [];
function pollEvents() {
if (!queue.length) {
return null;
}
const events = [];
while (queue.length && queue[0].length !== 3) {
push.call(events, shift.call(queue));
}
return events;
}
function write(event, promise) {
const entry = [event, null, promise];
push.call(queue, entry);
promise.then((data) => {
entry[1] = data;
pop.call(entry);
});
}
function getTestData(test) {
return {
type: test.type,
title: test.title,
timedOut: test.timedOut,
pending: test.pending,
duration: test.duration,
speed: test.speed,
state: test.state,
_fullTitle: test.fullTitle(),
_titlePath: test.titlePath(),
_slow: test.slow()
};
}
function forward(runner, event, processor) {
runner.on(event, (object, err) => {
write(event, processor(object, err));
});
}
function MochifyReporter(runner) {
const stats = runner.stats;
forward(runner, constants.EVENT_RUN_BEGIN, () =>
Promise.resolve({
start: stats.start.toISOString()
})
);
forward(runner, constants.EVENT_RUN_END, () =>
Promise.resolve({
end: stats.end.toISOString(),
duration: stats.duration
})
);
forward(runner, constants.EVENT_SUITE_BEGIN, (suite) =>
Promise.resolve({
root: suite.root,
title: suite.title,
pending: suite.pending,
delayed: suite.delayed
})
);
const resolveEmpty = () => Promise.resolve({});
forward(runner, constants.EVENT_SUITE_END, resolveEmpty);
forward(runner, constants.EVENT_DELAY_BEGIN, resolveEmpty);
forward(runner, constants.EVENT_DELAY_END, resolveEmpty);
const resolveTestData = (test) => Promise.resolve(getTestData(test));
forward(runner, constants.EVENT_TEST_PASS, resolveTestData);
forward(runner, constants.EVENT_TEST_PENDING, resolveTestData);
forward(runner, constants.EVENT_TEST_FAIL, (test, err) => {
const json = getTestData(test);
return new Promise((resolve) => {
serialize(err).then((serialized) => {
json.err = serialized;
resolve(json);
});
});
});
forward(runner, constants.EVENT_TEST_END, resolveTestData);
}
// @ts-ignore
mocha.reporter(MochifyReporter);
mocha.ui(/* MOCHIFY_UI */);
// @ts-ignore
mocha.timeout(/* MOCHIFY_TIMEOUT */);
// Workaround for https://github.com/mozilla/geckodriver/issues/1798
if (typeof globalThis !== 'undefined' && globalThis !== window) {
// Register globals on window. Mocha uses globalThis, if defined.
// @ts-ignore
window.mocha = mocha;
mocha.suite.emit('pre-require', window, null, mocha);
}
// @ts-ignore
mocha.mochify_pollEvents = pollEvents;
const chunks = [];
// @ts-ignore
mocha.mochify_receive = function (chunk) {
push.call(chunks, chunk);
};
// @ts-ignore
mocha.mochify_run = function () {
// Inject script
const s = document.createElement('script');
s.type = 'text/javascript';
s.textContent = chunks.join('');
document.body.appendChild(s);
// Run mocha
mocha.run((failures) => {
// @ts-ignore
if (typeof __coverage__ !== 'undefined') {
// @ts-ignore
write('mochify.coverage', Promise.resolve(window.__coverage__));
}
write('mochify.callback', Promise.resolve({ code: failures ? 1 : 0 }));
});
};
['debug', 'log', 'info', 'warn', 'error'].forEach((name) => {
if (console[name]) {
console[name] = function () {
write(
`console.${name}`,
Promise.all(map.call(slice.call(arguments), serialize))
);
};
}
});
window.onerror = function (msg, file, line, column, err) {
if (err) {
write('console.error', Promise.all([serialize(err)]));
} else {
write(
'console.error',
Promise.resolve([`${msg} at ${file}:${line}:${column}`])
);
}
};
window.onunhandledrejection = function (event) {
write(
'console.error',
Promise.all([
Promise.resolve('Unhandled rejection'),
serialize(event.reason)
])
);
};
const function_names = [
'Function',
'AsyncFunction',
'GeneratorFunction',
'AsyncGeneratorFunction'
];
const weak_refs = ['WeakSet', 'WeakMap', 'WeakRef'];
const array_names = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
];
const error_names = [
'Error',
'AggregateError',
'EvalError',
'RangeError',
'ReferenceError',
'SyntaxError',
'TypeError',
'URIError'
];
function serialize(input) {
const seen = new WeakMap();
return internal(input);
/* eslint-disable-next-line complexity */
async function internal(value, path = []) {
switch (typeof value) {
case 'undefined':
return ['undefined'];
case 'boolean':
return ['Boolean', value];
case 'number':
if (Number.isNaN(value)) {
return ['NaN'];
}
if (value === Infinity) {
return ['Infinity'];
}
if (value === -Infinity) {
return ['-Infinity'];
}
return ['Number', value];
case 'bigint':
return ['BigInt', String(value)];
case 'string':
return ['String', value];
case 'symbol':
return ['Symbol', value.description || ''];
case 'function': {
const str = value.toString();
if (str.startsWith('class ') && str.endsWith('}')) {
const proto = Object.getPrototypeOf(value);
if (proto === Function.prototype) {
return ['Class', value.name || ''];
}
return ['Class', value.name || '', proto.name || ''];
}
return [
function_names.includes(value.constructor.name)
? value.constructor.name
: 'Function',
value.name || '',
await keys(value, path),
await symbols(value, path)
];
}
case 'object': {
switch (true) {
case value === null:
return ['null'];
case seen.has(value):
return ['Circular', seen.get(value)];
case value instanceof Date: {
const ts = value.getTime();
return ['Date', Number.isNaN(ts) ? 'NaN' : ts];
}
case value instanceof RegExp:
return ['RegExp', value.source, value.flags];
case value instanceof Promise: {
const t = {};
const [state, result] = await Promise.race([value, t]).then(
(r) => (r === t ? ['pending'] : ['fulfilled', r]),
(r) => ['rejected', r]
);
if (state === 'pending') {
return ['Promise', state];
}
return ['Promise', state, await internal(result, path)];
}
case value instanceof Set:
return [
'Set',
await Promise.all(
map.call(Array.from(value), (v) => internal(v, path))
)
];
case value instanceof Map:
return [
'Map',
await Promise.all(
map.call(Array.from(value), async ([k, v]) => [
await internal(k, path),
await internal(v, path)
])
)
];
case value instanceof Attr:
return ['Attr', value.name, value.value];
case value instanceof Comment:
return ['Comment', value.data];
case value instanceof Element:
return [
'Element',
value.nodeName.toLowerCase(),
nodeMap(value.attributes),
await nodeList(value.childNodes, [...path, 'childNodes'])
];
case value instanceof ShadowRoot:
return ['ShadowRoot', await nodeList(value.childNodes, path)];
case value instanceof DocumentFragment:
return ['DocumentFragment', await nodeList(value.childNodes, path)];
case value instanceof HTMLCollection:
return ['HTMLCollection', await nodeList(value, path)];
case value instanceof NamedNodeMap:
return ['NamedNodeMap', nodeMap(value)];
case value instanceof NodeList:
return ['NodeList', await nodeList(value, path)];
case value instanceof Text:
return ['Text', value.data];
case value.constructor && weak_refs.includes(value.constructor.name):
return [value.constructor.name];
case value.constructor &&
array_names.includes(value.constructor.name):
return [value.constructor.name, Array.from(value)];
default: {
seen.set(value, path);
const mapped = [];
if (Array.isArray(value)) {
push.call(
mapped,
'Array',
await keys(value, path),
await symbols(value, path)
);
} else if (value instanceof Error) {
push.call(
mapped,
error_names.includes(value.constructor.name)
? value.constructor.name
: 'Error',
value.message,
value.stack,
await keys(value, path),
await symbols(value, path)
);
} else {
push.call(
mapped,
'Object',
await keys(value, path),
await symbols(value, path)
);
}
seen.delete(value);
return mapped;
}
}
}
default:
return ['Unknown', JSON.parse(JSON.stringify(value))];
}
}
function keys(obj, path) {
if (path.length > MAX_DEPTH) {
return Promise.resolve([]);
}
return Promise.all(
map.call(Object.entries(obj), async ([k, v]) => [
k,
await internal(v, [...path, k])
])
);
}
function symbols(obj, path) {
if (path.length > MAX_DEPTH) {
return Promise.resolve([]);
}
return Promise.all(
map.call(
filter.call(Object.getOwnPropertySymbols(obj), (symbol) =>
propertyIsEnumerable.call(obj, symbol)
),
async (symbol) => [
symbol.description || '',
await internal(obj[symbol], [...path, String(symbol)])
]
)
);
}
function nodeMap(value) {
const attrs = {};
forEach.call(Array.from(value), (attr) => {
attrs[attr.name] = attr.value;
});
return attrs;
}
function nodeList(value, path) {
return Promise.all(
map.call(Array.from(value), (node, i) => internal(node, [...path, i]))
);
}
}
/**
* @returns {string}
* @this {Object}
*/
function inspect() {
return this.toString();
}
[
Attr,
Comment,
Element,
DocumentFragment,
HTMLCollection,
NamedNodeMap,
NodeList
].forEach((Type) => {
Object.defineProperty(Type.prototype, 'inspect', { value: inspect });
});