suite.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var EventEmitter = require('events').EventEmitter;
  6. var Hook = require('./hook');
  7. var utils = require('./utils');
  8. var inherits = utils.inherits;
  9. var debug = require('debug')('mocha:suite');
  10. var milliseconds = require('./ms');
  11. /**
  12. * Expose `Suite`.
  13. */
  14. exports = module.exports = Suite;
  15. /**
  16. * Create a new `Suite` with the given `title` and parent `Suite`. When a suite
  17. * with the same title is already present, that suite is returned to provide
  18. * nicer reporter and more flexible meta-testing.
  19. *
  20. * @api public
  21. * @param {Suite} parent
  22. * @param {string} title
  23. * @return {Suite}
  24. */
  25. exports.create = function (parent, title) {
  26. var suite = new Suite(title, parent.ctx);
  27. suite.parent = parent;
  28. title = suite.fullTitle();
  29. parent.addSuite(suite);
  30. return suite;
  31. };
  32. /**
  33. * Initialize a new `Suite` with the given `title` and `ctx`.
  34. *
  35. * @api private
  36. * @param {string} title
  37. * @param {Context} parentContext
  38. */
  39. function Suite (title, parentContext) {
  40. if (!utils.isString(title)) {
  41. throw new Error('Suite `title` should be a "string" but "' + typeof title + '" was given instead.');
  42. }
  43. this.title = title;
  44. function Context () {}
  45. Context.prototype = parentContext;
  46. this.ctx = new Context();
  47. this.suites = [];
  48. this.tests = [];
  49. this.pending = false;
  50. this._beforeEach = [];
  51. this._beforeAll = [];
  52. this._afterEach = [];
  53. this._afterAll = [];
  54. this.root = !title;
  55. this._timeout = 20000;
  56. this._enableTimeouts = true;
  57. this._slow = 75;
  58. this._bail = false;
  59. this._retries = -1;
  60. this._onlyTests = [];
  61. this._onlySuites = [];
  62. this.delayed = false;
  63. }
  64. /**
  65. * Inherit from `EventEmitter.prototype`.
  66. */
  67. inherits(Suite, EventEmitter);
  68. /**
  69. * Return a clone of this `Suite`.
  70. *
  71. * @api private
  72. * @return {Suite}
  73. */
  74. Suite.prototype.clone = function () {
  75. var suite = new Suite(this.title);
  76. debug('clone');
  77. suite.ctx = this.ctx;
  78. suite.timeout(this.timeout());
  79. suite.retries(this.retries());
  80. suite.enableTimeouts(this.enableTimeouts());
  81. suite.slow(this.slow());
  82. suite.bail(this.bail());
  83. return suite;
  84. };
  85. /**
  86. * Set timeout `ms` or short-hand such as "2s".
  87. *
  88. * @api private
  89. * @param {number|string} ms
  90. * @return {Suite|number} for chaining
  91. */
  92. Suite.prototype.timeout = function (ms) {
  93. if (!arguments.length) {
  94. return this._timeout;
  95. }
  96. if (ms.toString() === '0') {
  97. this._enableTimeouts = false;
  98. }
  99. if (typeof ms === 'string') {
  100. ms = milliseconds(ms);
  101. }
  102. debug('timeout %d', ms);
  103. this._timeout = parseInt(ms, 10);
  104. return this;
  105. };
  106. /**
  107. * Set number of times to retry a failed test.
  108. *
  109. * @api private
  110. * @param {number|string} n
  111. * @return {Suite|number} for chaining
  112. */
  113. Suite.prototype.retries = function (n) {
  114. if (!arguments.length) {
  115. return this._retries;
  116. }
  117. debug('retries %d', n);
  118. this._retries = parseInt(n, 10) || 0;
  119. return this;
  120. };
  121. /**
  122. * Set timeout to `enabled`.
  123. *
  124. * @api private
  125. * @param {boolean} enabled
  126. * @return {Suite|boolean} self or enabled
  127. */
  128. Suite.prototype.enableTimeouts = function (enabled) {
  129. if (!arguments.length) {
  130. return this._enableTimeouts;
  131. }
  132. debug('enableTimeouts %s', enabled);
  133. this._enableTimeouts = enabled;
  134. return this;
  135. };
  136. /**
  137. * Set slow `ms` or short-hand such as "2s".
  138. *
  139. * @api private
  140. * @param {number|string} ms
  141. * @return {Suite|number} for chaining
  142. */
  143. Suite.prototype.slow = function (ms) {
  144. if (!arguments.length) {
  145. return this._slow;
  146. }
  147. if (typeof ms === 'string') {
  148. ms = milliseconds(ms);
  149. }
  150. debug('slow %d', ms);
  151. this._slow = ms;
  152. return this;
  153. };
  154. /**
  155. * Sets whether to bail after first error.
  156. *
  157. * @api private
  158. * @param {boolean} bail
  159. * @return {Suite|number} for chaining
  160. */
  161. Suite.prototype.bail = function (bail) {
  162. if (!arguments.length) {
  163. return this._bail;
  164. }
  165. debug('bail %s', bail);
  166. this._bail = bail;
  167. return this;
  168. };
  169. /**
  170. * Check if this suite or its parent suite is marked as pending.
  171. *
  172. * @api private
  173. */
  174. Suite.prototype.isPending = function () {
  175. return this.pending || (this.parent && this.parent.isPending());
  176. };
  177. /**
  178. * Run `fn(test[, done])` before running tests.
  179. *
  180. * @api private
  181. * @param {string} title
  182. * @param {Function} fn
  183. * @return {Suite} for chaining
  184. */
  185. Suite.prototype.beforeAll = function (title, fn) {
  186. if (this.isPending()) {
  187. return this;
  188. }
  189. if (typeof title === 'function') {
  190. fn = title;
  191. title = fn.name;
  192. }
  193. title = '"before all" hook' + (title ? ': ' + title : '');
  194. var hook = new Hook(title, fn);
  195. hook.parent = this;
  196. hook.timeout(this.timeout());
  197. hook.retries(this.retries());
  198. hook.enableTimeouts(this.enableTimeouts());
  199. hook.slow(this.slow());
  200. hook.ctx = this.ctx;
  201. this._beforeAll.push(hook);
  202. this.emit('beforeAll', hook);
  203. return this;
  204. };
  205. /**
  206. * Run `fn(test[, done])` after running tests.
  207. *
  208. * @api private
  209. * @param {string} title
  210. * @param {Function} fn
  211. * @return {Suite} for chaining
  212. */
  213. Suite.prototype.afterAll = function (title, fn) {
  214. if (this.isPending()) {
  215. return this;
  216. }
  217. if (typeof title === 'function') {
  218. fn = title;
  219. title = fn.name;
  220. }
  221. title = '"after all" hook' + (title ? ': ' + title : '');
  222. var hook = new Hook(title, fn);
  223. hook.parent = this;
  224. hook.timeout(this.timeout());
  225. hook.retries(this.retries());
  226. hook.enableTimeouts(this.enableTimeouts());
  227. hook.slow(this.slow());
  228. hook.ctx = this.ctx;
  229. this._afterAll.push(hook);
  230. this.emit('afterAll', hook);
  231. return this;
  232. };
  233. /**
  234. * Run `fn(test[, done])` before each test case.
  235. *
  236. * @api private
  237. * @param {string} title
  238. * @param {Function} fn
  239. * @return {Suite} for chaining
  240. */
  241. Suite.prototype.beforeEach = function (title, fn) {
  242. if (this.isPending()) {
  243. return this;
  244. }
  245. if (typeof title === 'function') {
  246. fn = title;
  247. title = fn.name;
  248. }
  249. title = '"before each" hook' + (title ? ': ' + title : '');
  250. var hook = new Hook(title, fn);
  251. hook.parent = this;
  252. hook.timeout(this.timeout());
  253. hook.retries(this.retries());
  254. hook.enableTimeouts(this.enableTimeouts());
  255. hook.slow(this.slow());
  256. hook.ctx = this.ctx;
  257. this._beforeEach.push(hook);
  258. this.emit('beforeEach', hook);
  259. return this;
  260. };
  261. /**
  262. * Run `fn(test[, done])` after each test case.
  263. *
  264. * @api private
  265. * @param {string} title
  266. * @param {Function} fn
  267. * @return {Suite} for chaining
  268. */
  269. Suite.prototype.afterEach = function (title, fn) {
  270. if (this.isPending()) {
  271. return this;
  272. }
  273. if (typeof title === 'function') {
  274. fn = title;
  275. title = fn.name;
  276. }
  277. title = '"after each" hook' + (title ? ': ' + title : '');
  278. var hook = new Hook(title, fn);
  279. hook.parent = this;
  280. hook.timeout(this.timeout());
  281. hook.retries(this.retries());
  282. hook.enableTimeouts(this.enableTimeouts());
  283. hook.slow(this.slow());
  284. hook.ctx = this.ctx;
  285. this._afterEach.push(hook);
  286. this.emit('afterEach', hook);
  287. return this;
  288. };
  289. /**
  290. * Add a test `suite`.
  291. *
  292. * @api private
  293. * @param {Suite} suite
  294. * @return {Suite} for chaining
  295. */
  296. Suite.prototype.addSuite = function (suite) {
  297. suite.parent = this;
  298. suite.timeout(this.timeout());
  299. suite.retries(this.retries());
  300. suite.enableTimeouts(this.enableTimeouts());
  301. suite.slow(this.slow());
  302. suite.bail(this.bail());
  303. this.suites.push(suite);
  304. this.emit('suite', suite);
  305. return this;
  306. };
  307. /**
  308. * Add a `test` to this suite.
  309. *
  310. * @api private
  311. * @param {Test} test
  312. * @return {Suite} for chaining
  313. */
  314. Suite.prototype.addTest = function (test) {
  315. test.parent = this;
  316. test.timeout(this.timeout());
  317. test.retries(this.retries());
  318. test.enableTimeouts(this.enableTimeouts());
  319. test.slow(this.slow());
  320. test.ctx = this.ctx;
  321. this.tests.push(test);
  322. this.emit('test', test);
  323. return this;
  324. };
  325. /**
  326. * Return the full title generated by recursively concatenating the parent's
  327. * full title.
  328. *
  329. * @api public
  330. * @return {string}
  331. */
  332. Suite.prototype.fullTitle = function () {
  333. if (this.parent) {
  334. var full = this.parent.fullTitle();
  335. if (full) {
  336. return full + ' ' + this.title;
  337. }
  338. }
  339. return this.title;
  340. };
  341. /**
  342. * Return the total number of tests.
  343. *
  344. * @api public
  345. * @return {number}
  346. */
  347. Suite.prototype.total = function () {
  348. return utils.reduce(this.suites, function (sum, suite) {
  349. return sum + suite.total();
  350. }, 0) + this.tests.length;
  351. };
  352. /**
  353. * Iterates through each suite recursively to find all tests. Applies a
  354. * function in the format `fn(test)`.
  355. *
  356. * @api private
  357. * @param {Function} fn
  358. * @return {Suite}
  359. */
  360. Suite.prototype.eachTest = function (fn) {
  361. utils.forEach(this.tests, fn);
  362. utils.forEach(this.suites, function (suite) {
  363. suite.eachTest(fn);
  364. });
  365. return this;
  366. };
  367. /**
  368. * This will run the root suite if we happen to be running in delayed mode.
  369. */
  370. Suite.prototype.run = function run () {
  371. if (this.root) {
  372. this.emit('run');
  373. }
  374. };