mocha.js 6.2 KB

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