1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | process.stdout = require('browser-stdout')({label: false});
|
11 |
|
12 | var parseQuery = require('./lib/browser/parse-query');
|
13 | var highlightTags = require('./lib/browser/highlight-tags');
|
14 | var Mocha = require('./lib/mocha');
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var mocha = new Mocha({reporter: 'html'});
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var Date = global.Date;
|
29 | var setTimeout = global.setTimeout;
|
30 | var setInterval = global.setInterval;
|
31 | var clearTimeout = global.clearTimeout;
|
32 | var clearInterval = global.clearInterval;
|
33 |
|
34 | var uncaughtExceptionHandlers = [];
|
35 |
|
36 | var originalOnerrorHandler = global.onerror;
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | process.removeListener = function (e, fn) {
|
44 | if (e === 'uncaughtException') {
|
45 | if (originalOnerrorHandler) {
|
46 | global.onerror = originalOnerrorHandler;
|
47 | } else {
|
48 | global.onerror = function () {};
|
49 | }
|
50 | var i = uncaughtExceptionHandlers.indexOf(fn);
|
51 | if (i !== -1) {
|
52 | uncaughtExceptionHandlers.splice(i, 1);
|
53 | }
|
54 | }
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | process.listenerCount = function (name) {
|
62 | if (name === 'uncaughtException') {
|
63 | return uncaughtExceptionHandlers.length;
|
64 | }
|
65 | return 0;
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | process.on = function (e, fn) {
|
73 | if (e === 'uncaughtException') {
|
74 | global.onerror = function (err, url, line) {
|
75 | fn(new Error(err + ' (' + url + ':' + line + ')'));
|
76 | return !mocha.options.allowUncaught;
|
77 | };
|
78 | uncaughtExceptionHandlers.push(fn);
|
79 | }
|
80 | };
|
81 |
|
82 | process.listeners = function (e) {
|
83 | if (e === 'uncaughtException') {
|
84 | return uncaughtExceptionHandlers;
|
85 | }
|
86 | return [];
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | mocha.suite.removeAllListeners('pre-require');
|
93 |
|
94 | var immediateQueue = [];
|
95 | var immediateTimeout;
|
96 |
|
97 | function timeslice() {
|
98 | var immediateStart = new Date().getTime();
|
99 | while (immediateQueue.length && new Date().getTime() - immediateStart < 100) {
|
100 | immediateQueue.shift()();
|
101 | }
|
102 | if (immediateQueue.length) {
|
103 | immediateTimeout = setTimeout(timeslice, 0);
|
104 | } else {
|
105 | immediateTimeout = null;
|
106 | }
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | Mocha.Runner.immediately = function (callback) {
|
114 | immediateQueue.push(callback);
|
115 | if (!immediateTimeout) {
|
116 | immediateTimeout = setTimeout(timeslice, 0);
|
117 | }
|
118 | };
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | mocha.throwError = function (err) {
|
126 | uncaughtExceptionHandlers.forEach(function (fn) {
|
127 | fn(err);
|
128 | });
|
129 | throw err;
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | mocha.ui = function (ui) {
|
138 | Mocha.prototype.ui.call(this, ui);
|
139 | this.suite.emit('pre-require', global, null, this);
|
140 | return this;
|
141 | };
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | mocha.setup = function (opts) {
|
148 | if (typeof opts === 'string') {
|
149 | opts = {ui: opts};
|
150 | }
|
151 | if (opts.delay === true) {
|
152 | this.delay();
|
153 | }
|
154 | var self = this;
|
155 | Object.keys(opts)
|
156 | .filter(function (opt) {
|
157 | return opt !== 'delay';
|
158 | })
|
159 | .forEach(function (opt) {
|
160 | if (Object.prototype.hasOwnProperty.call(opts, opt)) {
|
161 | self[opt](opts[opt]);
|
162 | }
|
163 | });
|
164 | return this;
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | mocha.run = function (fn) {
|
172 | var options = mocha.options;
|
173 | mocha.globals('location');
|
174 |
|
175 | var query = parseQuery(global.location.search || '');
|
176 | if (query.grep) {
|
177 | mocha.grep(query.grep);
|
178 | }
|
179 | if (query.fgrep) {
|
180 | mocha.fgrep(query.fgrep);
|
181 | }
|
182 | if (query.invert) {
|
183 | mocha.invert();
|
184 | }
|
185 |
|
186 | return Mocha.prototype.run.call(mocha, function (err) {
|
187 |
|
188 | var document = global.document;
|
189 | if (
|
190 | document &&
|
191 | document.getElementById('mocha') &&
|
192 | options.noHighlighting !== true
|
193 | ) {
|
194 | highlightTags('code');
|
195 | }
|
196 | if (fn) {
|
197 | fn(err);
|
198 | }
|
199 | });
|
200 | };
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | Mocha.process = process;
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | global.Mocha = Mocha;
|
213 | global.mocha = mocha;
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | [
|
219 | 'describe', 'context', 'it', 'specify',
|
220 | 'xdescribe', 'xcontext', 'xit', 'xspecify',
|
221 | 'before', 'beforeEach', 'afterEach', 'after'
|
222 | ].forEach(function(key) {
|
223 | mocha[key] = global[key];
|
224 | });
|
225 |
|
226 | module.exports = mocha;
|