UNPKG

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