1 | /*! |
---|
2 | * mocha |
---|
3 | * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca> |
---|
4 | * MIT Licensed |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * Module dependencies. |
---|
9 | */ |
---|
10 | |
---|
11 | var path = require('path') |
---|
12 | , utils = require('./utils'); |
---|
13 | |
---|
14 | /** |
---|
15 | * Expose `Mocha`. |
---|
16 | */ |
---|
17 | |
---|
18 | exports = module.exports = Mocha; |
---|
19 | |
---|
20 | /** |
---|
21 | * Expose internals. |
---|
22 | */ |
---|
23 | |
---|
24 | exports.utils = utils; |
---|
25 | exports.interfaces = require('./interfaces'); |
---|
26 | exports.reporters = require('./reporters'); |
---|
27 | exports.Runnable = require('./runnable'); |
---|
28 | exports.Context = require('./context'); |
---|
29 | exports.Runner = require('./runner'); |
---|
30 | exports.Suite = require('./suite'); |
---|
31 | exports.Hook = require('./hook'); |
---|
32 | exports.Test = require('./test'); |
---|
33 | |
---|
34 | /** |
---|
35 | * Return image `name` path. |
---|
36 | * |
---|
37 | * @param {String} name |
---|
38 | * @return {String} |
---|
39 | * @api private |
---|
40 | */ |
---|
41 | |
---|
42 | function image(name) { |
---|
43 | return __dirname + '/../images/' + name + '.png'; |
---|
44 | } |
---|
45 | |
---|
46 | /** |
---|
47 | * Setup mocha with `options`. |
---|
48 | * |
---|
49 | * Options: |
---|
50 | * |
---|
51 | * - `ui` name "bdd", "tdd", "exports" etc |
---|
52 | * - `reporter` reporter instance, defaults to `mocha.reporters.Dot` |
---|
53 | * - `globals` array of accepted globals |
---|
54 | * - `timeout` timeout in milliseconds |
---|
55 | * - `bail` bail on the first test failure |
---|
56 | * - `slow` milliseconds to wait before considering a test slow |
---|
57 | * - `ignoreLeaks` ignore global leaks |
---|
58 | * - `grep` string or regexp to filter tests with |
---|
59 | * |
---|
60 | * @param {Object} options |
---|
61 | * @api public |
---|
62 | */ |
---|
63 | |
---|
64 | function Mocha(options) { |
---|
65 | options = options || {}; |
---|
66 | this.files = []; |
---|
67 | this.options = options; |
---|
68 | this.grep(options.grep); |
---|
69 | this.suite = new exports.Suite('', new exports.Context); |
---|
70 | this.ui(options.ui); |
---|
71 | this.bail(options.bail); |
---|
72 | this.reporter(options.reporter); |
---|
73 | if (null != options.timeout) this.timeout(options.timeout); |
---|
74 | this.useColors(options.useColors) |
---|
75 | if (options.slow) this.slow(options.slow); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Enable or disable bailing on the first failure. |
---|
80 | * |
---|
81 | * @param {Boolean} [bail] |
---|
82 | * @api public |
---|
83 | */ |
---|
84 | |
---|
85 | Mocha.prototype.bail = function(bail){ |
---|
86 | if (0 == arguments.length) bail = true; |
---|
87 | this.suite.bail(bail); |
---|
88 | return this; |
---|
89 | }; |
---|
90 | |
---|
91 | /** |
---|
92 | * Add test `file`. |
---|
93 | * |
---|
94 | * @param {String} file |
---|
95 | * @api public |
---|
96 | */ |
---|
97 | |
---|
98 | Mocha.prototype.addFile = function(file){ |
---|
99 | this.files.push(file); |
---|
100 | return this; |
---|
101 | }; |
---|
102 | |
---|
103 | /** |
---|
104 | * Set reporter to `reporter`, defaults to "dot". |
---|
105 | * |
---|
106 | * @param {String|Function} reporter name or constructor |
---|
107 | * @api public |
---|
108 | */ |
---|
109 | |
---|
110 | Mocha.prototype.reporter = function(reporter){ |
---|
111 | if ('function' == typeof reporter) { |
---|
112 | this._reporter = reporter; |
---|
113 | } else { |
---|
114 | reporter = reporter || 'dot'; |
---|
115 | var _reporter; |
---|
116 | try { _reporter = require('./reporters/' + reporter); } catch (err) {}; |
---|
117 | if (!_reporter) try { _reporter = require(reporter); } catch (err) {}; |
---|
118 | if (!_reporter && reporter === 'teamcity') |
---|
119 | console.warn('The Teamcity reporter was moved to a package named ' + |
---|
120 | 'mocha-teamcity-reporter ' + |
---|
121 | '(https://npmjs.org/package/mocha-teamcity-reporter).'); |
---|
122 | if (!_reporter) throw new Error('invalid reporter "' + reporter + '"'); |
---|
123 | this._reporter = _reporter; |
---|
124 | } |
---|
125 | return this; |
---|
126 | }; |
---|
127 | |
---|
128 | /** |
---|
129 | * Set test UI `name`, defaults to "bdd". |
---|
130 | * |
---|
131 | * @param {String} bdd |
---|
132 | * @api public |
---|
133 | */ |
---|
134 | |
---|
135 | Mocha.prototype.ui = function(name){ |
---|
136 | name = name || 'bdd'; |
---|
137 | this._ui = exports.interfaces[name]; |
---|
138 | if (!this._ui) try { this._ui = require(name); } catch (err) {}; |
---|
139 | if (!this._ui) throw new Error('invalid interface "' + name + '"'); |
---|
140 | this._ui = this._ui(this.suite); |
---|
141 | return this; |
---|
142 | }; |
---|
143 | |
---|
144 | /** |
---|
145 | * Load registered files. |
---|
146 | * |
---|
147 | * @api private |
---|
148 | */ |
---|
149 | |
---|
150 | Mocha.prototype.loadFiles = function(fn){ |
---|
151 | var self = this; |
---|
152 | var suite = this.suite; |
---|
153 | var pending = this.files.length; |
---|
154 | this.files.forEach(function(file){ |
---|
155 | file = path.resolve(file); |
---|
156 | suite.emit('pre-require', global, file, self); |
---|
157 | suite.emit('require', require(file), file, self); |
---|
158 | suite.emit('post-require', global, file, self); |
---|
159 | --pending || (fn && fn()); |
---|
160 | }); |
---|
161 | }; |
---|
162 | |
---|
163 | /** |
---|
164 | * Enable growl support. |
---|
165 | * |
---|
166 | * @api private |
---|
167 | */ |
---|
168 | |
---|
169 | Mocha.prototype._growl = function(runner, reporter) { |
---|
170 | var notify = require('growl'); |
---|
171 | |
---|
172 | runner.on('end', function(){ |
---|
173 | var stats = reporter.stats; |
---|
174 | if (stats.failures) { |
---|
175 | var msg = stats.failures + ' of ' + runner.total + ' tests failed'; |
---|
176 | notify(msg, { name: 'mocha', title: 'Failed', image: image('error') }); |
---|
177 | } else { |
---|
178 | notify(stats.passes + ' tests passed in ' + stats.duration + 'ms', { |
---|
179 | name: 'mocha' |
---|
180 | , title: 'Passed' |
---|
181 | , image: image('ok') |
---|
182 | }); |
---|
183 | } |
---|
184 | }); |
---|
185 | }; |
---|
186 | |
---|
187 | /** |
---|
188 | * Add regexp to grep, if `re` is a string it is escaped. |
---|
189 | * |
---|
190 | * @param {RegExp|String} re |
---|
191 | * @return {Mocha} |
---|
192 | * @api public |
---|
193 | */ |
---|
194 | |
---|
195 | Mocha.prototype.grep = function(re){ |
---|
196 | this.options.grep = 'string' == typeof re |
---|
197 | ? new RegExp(utils.escapeRegexp(re)) |
---|
198 | : re; |
---|
199 | return this; |
---|
200 | }; |
---|
201 | |
---|
202 | /** |
---|
203 | * Invert `.grep()` matches. |
---|
204 | * |
---|
205 | * @return {Mocha} |
---|
206 | * @api public |
---|
207 | */ |
---|
208 | |
---|
209 | Mocha.prototype.invert = function(){ |
---|
210 | this.options.invert = true; |
---|
211 | return this; |
---|
212 | }; |
---|
213 | |
---|
214 | /** |
---|
215 | * Ignore global leaks. |
---|
216 | * |
---|
217 | * @param {Boolean} ignore |
---|
218 | * @return {Mocha} |
---|
219 | * @api public |
---|
220 | */ |
---|
221 | |
---|
222 | Mocha.prototype.ignoreLeaks = function(ignore){ |
---|
223 | this.options.ignoreLeaks = !!ignore; |
---|
224 | return this; |
---|
225 | }; |
---|
226 | |
---|
227 | /** |
---|
228 | * Enable global leak checking. |
---|
229 | * |
---|
230 | * @return {Mocha} |
---|
231 | * @api public |
---|
232 | */ |
---|
233 | |
---|
234 | Mocha.prototype.checkLeaks = function(){ |
---|
235 | this.options.ignoreLeaks = false; |
---|
236 | return this; |
---|
237 | }; |
---|
238 | |
---|
239 | /** |
---|
240 | * Enable growl support. |
---|
241 | * |
---|
242 | * @return {Mocha} |
---|
243 | * @api public |
---|
244 | */ |
---|
245 | |
---|
246 | Mocha.prototype.growl = function(){ |
---|
247 | this.options.growl = true; |
---|
248 | return this; |
---|
249 | }; |
---|
250 | |
---|
251 | /** |
---|
252 | * Ignore `globals` array or string. |
---|
253 | * |
---|
254 | * @param {Array|String} globals |
---|
255 | * @return {Mocha} |
---|
256 | * @api public |
---|
257 | */ |
---|
258 | |
---|
259 | Mocha.prototype.globals = function(globals){ |
---|
260 | this.options.globals = (this.options.globals || []).concat(globals); |
---|
261 | return this; |
---|
262 | }; |
---|
263 | |
---|
264 | /** |
---|
265 | * Emit color output. |
---|
266 | * |
---|
267 | * @param {Boolean} colors |
---|
268 | * @return {Mocha} |
---|
269 | * @api public |
---|
270 | */ |
---|
271 | |
---|
272 | Mocha.prototype.useColors = function(colors){ |
---|
273 | this.options.useColors = arguments.length && colors != undefined |
---|
274 | ? colors |
---|
275 | : true; |
---|
276 | return this; |
---|
277 | }; |
---|
278 | |
---|
279 | /** |
---|
280 | * Set the timeout in milliseconds. |
---|
281 | * |
---|
282 | * @param {Number} timeout |
---|
283 | * @return {Mocha} |
---|
284 | * @api public |
---|
285 | */ |
---|
286 | |
---|
287 | Mocha.prototype.timeout = function(timeout){ |
---|
288 | this.suite.timeout(timeout); |
---|
289 | return this; |
---|
290 | }; |
---|
291 | |
---|
292 | /** |
---|
293 | * Set slowness threshold in milliseconds. |
---|
294 | * |
---|
295 | * @param {Number} slow |
---|
296 | * @return {Mocha} |
---|
297 | * @api public |
---|
298 | */ |
---|
299 | |
---|
300 | Mocha.prototype.slow = function(slow){ |
---|
301 | this.suite.slow(slow); |
---|
302 | return this; |
---|
303 | }; |
---|
304 | |
---|
305 | /** |
---|
306 | * Makes all tests async (accepting a callback) |
---|
307 | * |
---|
308 | * @return {Mocha} |
---|
309 | * @api public |
---|
310 | */ |
---|
311 | |
---|
312 | Mocha.prototype.asyncOnly = function(){ |
---|
313 | this.options.asyncOnly = true; |
---|
314 | return this; |
---|
315 | }; |
---|
316 | |
---|
317 | /** |
---|
318 | * Run tests and invoke `fn()` when complete. |
---|
319 | * |
---|
320 | * @param {Function} fn |
---|
321 | * @return {Runner} |
---|
322 | * @api public |
---|
323 | */ |
---|
324 | |
---|
325 | Mocha.prototype.run = function(fn){ |
---|
326 | if (this.files.length) this.loadFiles(); |
---|
327 | var suite = this.suite; |
---|
328 | var options = this.options; |
---|
329 | var runner = new exports.Runner(suite); |
---|
330 | var reporter = new this._reporter(runner); |
---|
331 | runner.ignoreLeaks = false !== options.ignoreLeaks; |
---|
332 | runner.asyncOnly = options.asyncOnly; |
---|
333 | if (options.grep) runner.grep(options.grep, options.invert); |
---|
334 | if (options.globals) runner.globals(options.globals); |
---|
335 | if (options.growl) this._growl(runner, reporter); |
---|
336 | exports.reporters.Base.useColors = options.useColors; |
---|
337 | return runner.run(fn); |
---|
338 | }; |
---|