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