-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
shim.js
150 lines (126 loc) · 3.93 KB
/
shim.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
(function shim() {
Object.defineProperty(window, 'mocha', {
get() {
return undefined;
},
set(m) {
shimMocha(m);
delete window.mocha;
window.mocha = m;
// mochaOptions is injected in lib/client.js
mochaOptions = mochaOptions || {
ui: 'bdd',
reporter: 'spec',
colors: true
};
mocha.setup(mochaOptions);
},
configurable: true
});
Object.defineProperty(window, 'Mocha', {
get() {
return undefined;
},
set(m) {
delete window.Mocha;
window.Mocha = m;
m.process.stdout._write = function mwrite(chunks, encoding, cb) {
const output = chunks.toString ? chunks.toString() : chunks;
window._eventbus.emit('mocha', output);
m.process.nextTick(cb);
};
window._eventbus.emit('width');
},
configurable: true
});
function shimMocha(m) {
const origRun = m.run;
const origUi = m.ui;
m.ui = function mui() {
const retval = origUi.apply(mocha, arguments);
m.reporter = () => {};
return retval;
};
m.run = function mrun() {
window._eventbus.emit('started', m.suite.suites.length);
m.runner = origRun.apply(mocha, arguments);
if (m.runner.stats && m.runner.stats.end) {
window._eventbus.emit('ended', m.runner.stats);
} else {
m.runner.on('end', () => {
window._eventbus.emit('ended', m.runner.stats);
});
}
return m.runner;
};
}
// Mocha needs the formating feature of console.log so copy node's format function and
// monkey-patch it into place. This code is copied from node's, links copyright applies.
// https://github.com/joyent/node/blob/master/lib/util.js
if (!console.format) {
const origError = console.error;
const origLog = console.log;
// stringify() and serizlier() from https://github.com/moll/json-stringify-safe
function stringify(obj, replacer, spaces, cycleReplacer) {
if (typeof replacer !== 'function') {
replacer = null;
}
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
}
function serializer(replacer, cycleReplacer) {
const stack = [];
const keys = [];
if (cycleReplacer == null)
cycleReplacer = function(key, value) {
if (stack[0] === value) return '[Circular ~]';
return `[Circular ~.${keys.slice(0, stack.indexOf(value)).join('.')}]`;
};
return function(key, value) {
if (stack.length > 0) {
const thisPos = stack.indexOf(this);
~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value);
} else stack.push(value);
return replacer == null ? value : replacer.call(this, key, value);
};
}
console.format = function(f) {
if (typeof f !== 'string') {
return Array.prototype.map.call(arguments, stringify).join(' ');
}
let i = 1;
const args = arguments;
const len = args.length;
let str = String(f).replace(/%[sdj%]/g, (x) => {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
case '%s':
return String(args[i++]);
case '%d':
return Number(args[i++]);
case '%j':
return stringify(args[i++]);
default:
return x;
}
});
let x;
for (x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ` ${x}`;
} else {
str += ` ${stringify(x)}`;
}
}
return str;
};
console.error = function() {
origError.call(console, console.format(...arguments));
};
console.log = function() {
origLog.call(console, console.format(...arguments));
};
}
})();