UNPKG

4.03 kBJavaScriptView Raw
1(function shim() {
2 Object.defineProperty(window, 'mocha', {
3 get() {
4 return undefined;
5 },
6 set(m) {
7 shimMocha(m);
8 delete window.mocha;
9 window.mocha = m;
10
11 // mochaOptions is injected in lib/client.js
12 mochaOptions = mochaOptions || {
13 ui: 'bdd',
14 reporter: 'spec',
15 useColors: true
16 };
17
18 mocha.setup(mochaOptions);
19 },
20 configurable: true
21 });
22
23 Object.defineProperty(window, 'Mocha', {
24 get() {
25 return undefined;
26 },
27 set(m) {
28 delete window.Mocha;
29 window.Mocha = m;
30
31 m.process.stdout._write = function mwrite(chunks, encoding, cb) {
32 const output = chunks.toString ? chunks.toString() : chunks;
33
34 window._eventbus.emit('mocha', output);
35
36 m.process.nextTick(cb);
37 };
38
39 window._eventbus.emit('width');
40 },
41 configurable: true
42 });
43
44 function shimMocha(m) {
45 const origRun = m.run;
46 const origUi = m.ui;
47
48 m.ui = function mui() {
49 const retval = origUi.apply(mocha, arguments);
50 m.reporter = () => {};
51 return retval;
52 };
53 m.run = function mrun() {
54 window._eventbus.emit('started', m.suite.suites.length);
55
56 m.runner = origRun.apply(mocha, arguments);
57 if (m.runner.stats && m.runner.stats.end) {
58 window._eventbus.emit('ended', m.runner.stats);
59 } else {
60 m.runner.on('end', () => {
61 window._eventbus.emit('ended', m.runner.stats);
62 });
63 }
64 return m.runner;
65 };
66 }
67
68 // Mocha needs the formating feature of console.log so copy node's format function and
69 // monkey-patch it into place. This code is copied from node's, links copyright applies.
70 // https://github.com/joyent/node/blob/master/lib/util.js
71 if (!console.format) {
72 const origError = console.error;
73
74 const origLog = console.log;
75
76 // stringify() and serizlier() from https://github.com/moll/json-stringify-safe
77 function stringify(obj, replacer, spaces, cycleReplacer) {
78 if (typeof replacer !== 'function') {
79 replacer = null;
80 }
81 return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
82 }
83
84 function serializer(replacer, cycleReplacer) {
85 const stack = [];
86 const keys = [];
87
88 if (cycleReplacer == null)
89 cycleReplacer = function(key, value) {
90 if (stack[0] === value) return '[Circular ~]';
91 return `[Circular ~.${keys.slice(0, stack.indexOf(value)).join('.')}]`;
92 };
93
94 return function(key, value) {
95 if (stack.length > 0) {
96 const thisPos = stack.indexOf(this);
97 ~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
98 ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
99 if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value);
100 } else stack.push(value);
101
102 return replacer == null ? value : replacer.call(this, key, value);
103 };
104 }
105
106 console.format = function(f) {
107 if (typeof f !== 'string') {
108 return Array.prototype.map.call(arguments, stringify).join(' ');
109 }
110 let i = 1;
111
112 const args = arguments;
113
114 const len = args.length;
115
116 let str = String(f).replace(/%[sdj%]/g, (x) => {
117 if (x === '%%') return '%';
118 if (i >= len) return x;
119 switch (x) {
120 case '%s':
121 return String(args[i++]);
122 case '%d':
123 return Number(args[i++]);
124 case '%j':
125 return stringify(args[i++]);
126 default:
127 return x;
128 }
129 });
130
131 let x;
132 for (x = args[i]; i < len; x = args[++i]) {
133 if (x === null || typeof x !== 'object') {
134 str += ` ${x}`;
135 } else {
136 str += ` ${stringify(x)}`;
137 }
138 }
139 return str;
140 };
141
142 console.error = function() {
143 origError.call(console, console.format(...arguments));
144 };
145
146 console.log = function() {
147 origLog.call(console, console.format(...arguments));
148 };
149 }
150})();