runner.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. 'use strict';
  2. /**
  3. * Module dependencies.
  4. */
  5. var EventEmitter = require('events').EventEmitter;
  6. var Pending = require('./pending');
  7. var utils = require('./utils');
  8. var inherits = utils.inherits;
  9. var debug = require('debug')('mocha:runner');
  10. var Runnable = require('./runnable');
  11. var filter = utils.filter;
  12. var indexOf = utils.indexOf;
  13. var some = utils.some;
  14. var keys = utils.keys;
  15. var stackFilter = utils.stackTraceFilter();
  16. var stringify = utils.stringify;
  17. var type = utils.type;
  18. var undefinedError = utils.undefinedError;
  19. var isArray = utils.isArray;
  20. /**
  21. * Non-enumerable globals.
  22. */
  23. var globals = [
  24. 'setTimeout',
  25. 'clearTimeout',
  26. 'setInterval',
  27. 'clearInterval',
  28. 'XMLHttpRequest',
  29. 'Date',
  30. 'setImmediate',
  31. 'clearImmediate'
  32. ];
  33. /**
  34. * Expose `Runner`.
  35. */
  36. module.exports = Runner;
  37. /**
  38. * Initialize a `Runner` for the given `suite`.
  39. *
  40. * Events:
  41. *
  42. * - `start` execution started
  43. * - `end` execution complete
  44. * - `suite` (suite) test suite execution started
  45. * - `suite end` (suite) all tests (and sub-suites) have finished
  46. * - `test` (test) test execution started
  47. * - `test end` (test) test completed
  48. * - `hook` (hook) hook execution started
  49. * - `hook end` (hook) hook complete
  50. * - `pass` (test) test passed
  51. * - `fail` (test, err) test failed
  52. * - `pending` (test) test pending
  53. *
  54. * @api public
  55. * @param {Suite} suite Root suite
  56. * @param {boolean} [delay] Whether or not to delay execution of root suite
  57. * until ready.
  58. */
  59. function Runner (suite, delay) {
  60. var self = this;
  61. this._globals = [];
  62. this._abort = false;
  63. this._delay = delay;
  64. this.suite = suite;
  65. this.started = false;
  66. this.total = suite.total();
  67. this.failures = 0;
  68. this.on('test end', function (test) {
  69. self.checkGlobals(test);
  70. });
  71. this.on('hook end', function (hook) {
  72. self.checkGlobals(hook);
  73. });
  74. this._defaultGrep = /.*/;
  75. this.grep(this._defaultGrep);
  76. this.globals(this.globalProps().concat(extraGlobals()));
  77. }
  78. /**
  79. * Wrapper for setImmediate, process.nextTick, or browser polyfill.
  80. *
  81. * @param {Function} fn
  82. * @api private
  83. */
  84. Runner.immediately = global.setImmediate || process.nextTick;
  85. /**
  86. * Inherit from `EventEmitter.prototype`.
  87. */
  88. inherits(Runner, EventEmitter);
  89. /**
  90. * Run tests with full titles matching `re`. Updates runner.total
  91. * with number of tests matched.
  92. *
  93. * @param {RegExp} re
  94. * @param {Boolean} invert
  95. * @return {Runner} for chaining
  96. * @api public
  97. * @param {RegExp} re
  98. * @param {boolean} invert
  99. * @return {Runner} Runner instance.
  100. */
  101. Runner.prototype.grep = function (re, invert) {
  102. debug('grep %s', re);
  103. this._grep = re;
  104. this._invert = invert;
  105. this.total = this.grepTotal(this.suite);
  106. return this;
  107. };
  108. /**
  109. * Returns the number of tests matching the grep search for the
  110. * given suite.
  111. *
  112. * @param {Suite} suite
  113. * @return {Number}
  114. * @api public
  115. * @param {Suite} suite
  116. * @return {number}
  117. */
  118. Runner.prototype.grepTotal = function (suite) {
  119. var self = this;
  120. var total = 0;
  121. suite.eachTest(function (test) {
  122. var match = self._grep.test(test.fullTitle());
  123. if (self._invert) {
  124. match = !match;
  125. }
  126. if (match) {
  127. total++;
  128. }
  129. });
  130. return total;
  131. };
  132. /**
  133. * Return a list of global properties.
  134. *
  135. * @return {Array}
  136. * @api private
  137. */
  138. Runner.prototype.globalProps = function () {
  139. var props = keys(global);
  140. // non-enumerables
  141. for (var i = 0; i < globals.length; ++i) {
  142. if (~indexOf(props, globals[i])) {
  143. continue;
  144. }
  145. props.push(globals[i]);
  146. }
  147. return props;
  148. };
  149. /**
  150. * Allow the given `arr` of globals.
  151. *
  152. * @param {Array} arr
  153. * @return {Runner} for chaining
  154. * @api public
  155. * @param {Array} arr
  156. * @return {Runner} Runner instance.
  157. */
  158. Runner.prototype.globals = function (arr) {
  159. if (!arguments.length) {
  160. return this._globals;
  161. }
  162. debug('globals %j', arr);
  163. this._globals = this._globals.concat(arr);
  164. return this;
  165. };
  166. /**
  167. * Check for global variable leaks.
  168. *
  169. * @api private
  170. */
  171. Runner.prototype.checkGlobals = function (test) {
  172. if (this.ignoreLeaks) {
  173. return;
  174. }
  175. var ok = this._globals;
  176. var globals = this.globalProps();
  177. var leaks;
  178. if (test) {
  179. ok = ok.concat(test._allowedGlobals || []);
  180. }
  181. if (this.prevGlobalsLength === globals.length) {
  182. return;
  183. }
  184. this.prevGlobalsLength = globals.length;
  185. leaks = filterLeaks(ok, globals);
  186. this._globals = this._globals.concat(leaks);
  187. if (leaks.length > 1) {
  188. this.fail(test, new Error('global leaks detected: ' + leaks.join(', ') + ''));
  189. } else if (leaks.length) {
  190. this.fail(test, new Error('global leak detected: ' + leaks[0]));
  191. }
  192. };
  193. /**
  194. * Fail the given `test`.
  195. *
  196. * @api private
  197. * @param {Test} test
  198. * @param {Error} err
  199. */
  200. Runner.prototype.fail = function (test, err) {
  201. if (test.isPending()) {
  202. return;
  203. }
  204. ++this.failures;
  205. test.state = 'failed';
  206. if (!(err instanceof Error || err && typeof err.message === 'string')) {
  207. err = new Error('the ' + type(err) + ' ' + stringify(err) + ' was thrown, throw an Error :)');
  208. }
  209. try {
  210. err.stack = (this.fullStackTrace || !err.stack)
  211. ? err.stack
  212. : stackFilter(err.stack);
  213. } catch (ignored) {
  214. // some environments do not take kindly to monkeying with the stack
  215. }
  216. this.emit('fail', test, err);
  217. };
  218. /**
  219. * Fail the given `hook` with `err`.
  220. *
  221. * Hook failures work in the following pattern:
  222. * - If bail, then exit
  223. * - Failed `before` hook skips all tests in a suite and subsuites,
  224. * but jumps to corresponding `after` hook
  225. * - Failed `before each` hook skips remaining tests in a
  226. * suite and jumps to corresponding `after each` hook,
  227. * which is run only once
  228. * - Failed `after` hook does not alter
  229. * execution order
  230. * - Failed `after each` hook skips remaining tests in a
  231. * suite and subsuites, but executes other `after each`
  232. * hooks
  233. *
  234. * @api private
  235. * @param {Hook} hook
  236. * @param {Error} err
  237. */
  238. Runner.prototype.failHook = function (hook, err) {
  239. if (hook.ctx && hook.ctx.currentTest) {
  240. hook.originalTitle = hook.originalTitle || hook.title;
  241. hook.title = hook.originalTitle + ' for "' + hook.ctx.currentTest.title + '"';
  242. }
  243. this.fail(hook, err);
  244. if (this.suite.bail()) {
  245. this.emit('end');
  246. }
  247. };
  248. /**
  249. * Run hook `name` callbacks and then invoke `fn()`.
  250. *
  251. * @api private
  252. * @param {string} name
  253. * @param {Function} fn
  254. */
  255. Runner.prototype.hook = function (name, fn) {
  256. var suite = this.suite;
  257. var hooks = suite['_' + name];
  258. var self = this;
  259. function next (i) {
  260. var hook = hooks[i];
  261. if (!hook) {
  262. return fn();
  263. }
  264. self.currentRunnable = hook;
  265. hook.ctx.currentTest = self.test;
  266. self.emit('hook', hook);
  267. if (!hook.listeners('error').length) {
  268. hook.on('error', function (err) {
  269. self.failHook(hook, err);
  270. });
  271. }
  272. hook.run(function (err) {
  273. var testError = hook.error();
  274. if (testError) {
  275. self.fail(self.test, testError);
  276. }
  277. if (err) {
  278. if (err instanceof Pending) {
  279. if (name === 'beforeEach' || name === 'afterEach') {
  280. self.test.pending = true;
  281. } else {
  282. utils.forEach(suite.tests, function (test) {
  283. test.pending = true;
  284. });
  285. // a pending hook won't be executed twice.
  286. hook.pending = true;
  287. }
  288. } else {
  289. self.failHook(hook, err);
  290. // stop executing hooks, notify callee of hook err
  291. return fn(err);
  292. }
  293. }
  294. self.emit('hook end', hook);
  295. delete hook.ctx.currentTest;
  296. next(++i);
  297. });
  298. }
  299. Runner.immediately(function () {
  300. next(0);
  301. });
  302. };
  303. /**
  304. * Run hook `name` for the given array of `suites`
  305. * in order, and callback `fn(err, errSuite)`.
  306. *
  307. * @api private
  308. * @param {string} name
  309. * @param {Array} suites
  310. * @param {Function} fn
  311. */
  312. Runner.prototype.hooks = function (name, suites, fn) {
  313. var self = this;
  314. var orig = this.suite;
  315. function next (suite) {
  316. self.suite = suite;
  317. if (!suite) {
  318. self.suite = orig;
  319. return fn();
  320. }
  321. self.hook(name, function (err) {
  322. if (err) {
  323. var errSuite = self.suite;
  324. self.suite = orig;
  325. return fn(err, errSuite);
  326. }
  327. next(suites.pop());
  328. });
  329. }
  330. next(suites.pop());
  331. };
  332. /**
  333. * Run hooks from the top level down.
  334. *
  335. * @param {String} name
  336. * @param {Function} fn
  337. * @api private
  338. */
  339. Runner.prototype.hookUp = function (name, fn) {
  340. var suites = [this.suite].concat(this.parents()).reverse();
  341. this.hooks(name, suites, fn);
  342. };
  343. /**
  344. * Run hooks from the bottom up.
  345. *
  346. * @param {String} name
  347. * @param {Function} fn
  348. * @api private
  349. */
  350. Runner.prototype.hookDown = function (name, fn) {
  351. var suites = [this.suite].concat(this.parents());
  352. this.hooks(name, suites, fn);
  353. };
  354. /**
  355. * Return an array of parent Suites from
  356. * closest to furthest.
  357. *
  358. * @return {Array}
  359. * @api private
  360. */
  361. Runner.prototype.parents = function () {
  362. var suite = this.suite;
  363. var suites = [];
  364. while (suite.parent) {
  365. suite = suite.parent;
  366. suites.push(suite);
  367. }
  368. return suites;
  369. };
  370. /**
  371. * Run the current test and callback `fn(err)`.
  372. *
  373. * @param {Function} fn
  374. * @api private
  375. */
  376. Runner.prototype.runTest = function (fn) {
  377. var self = this;
  378. var test = this.test;
  379. if (!test) {
  380. return;
  381. }
  382. if (this.asyncOnly) {
  383. test.asyncOnly = true;
  384. }
  385. if (this.allowUncaught) {
  386. test.allowUncaught = true;
  387. return test.run(fn);
  388. }
  389. try {
  390. test.on('error', function (err) {
  391. self.fail(test, err);
  392. });
  393. test.run(fn);
  394. } catch (err) {
  395. fn(err);
  396. }
  397. };
  398. /**
  399. * Run tests in the given `suite` and invoke the callback `fn()` when complete.
  400. *
  401. * @api private
  402. * @param {Suite} suite
  403. * @param {Function} fn
  404. */
  405. Runner.prototype.runTests = function (suite, fn) {
  406. var self = this;
  407. var tests = suite.tests.slice();
  408. var test;
  409. function hookErr (_, errSuite, after) {
  410. // before/after Each hook for errSuite failed:
  411. var orig = self.suite;
  412. // for failed 'after each' hook start from errSuite parent,
  413. // otherwise start from errSuite itself
  414. self.suite = after ? errSuite.parent : errSuite;
  415. if (self.suite) {
  416. // call hookUp afterEach
  417. self.hookUp('afterEach', function (err2, errSuite2) {
  418. self.suite = orig;
  419. // some hooks may fail even now
  420. if (err2) {
  421. return hookErr(err2, errSuite2, true);
  422. }
  423. // report error suite
  424. fn(errSuite);
  425. });
  426. } else {
  427. // there is no need calling other 'after each' hooks
  428. self.suite = orig;
  429. fn(errSuite);
  430. }
  431. }
  432. function next (err, errSuite) {
  433. // if we bail after first err
  434. if (self.failures && suite._bail) {
  435. return fn();
  436. }
  437. if (self._abort) {
  438. return fn();
  439. }
  440. if (err) {
  441. return hookErr(err, errSuite, true);
  442. }
  443. // next test
  444. test = tests.shift();
  445. // all done
  446. if (!test) {
  447. return fn();
  448. }
  449. // grep
  450. var match = self._grep.test(test.fullTitle());
  451. if (self._invert) {
  452. match = !match;
  453. }
  454. if (!match) {
  455. // Run immediately only if we have defined a grep. When we
  456. // define a grep — It can cause maximum callstack error if
  457. // the grep is doing a large recursive loop by neglecting
  458. // all tests. The run immediately function also comes with
  459. // a performance cost. So we don't want to run immediately
  460. // if we run the whole test suite, because running the whole
  461. // test suite don't do any immediate recursive loops. Thus,
  462. // allowing a JS runtime to breathe.
  463. if (self._grep !== self._defaultGrep) {
  464. Runner.immediately(next);
  465. } else {
  466. next();
  467. }
  468. return;
  469. }
  470. if (test.isPending()) {
  471. self.emit('pending', test);
  472. self.emit('test end', test);
  473. return next();
  474. }
  475. // execute test and hook(s)
  476. self.emit('test', self.test = test);
  477. self.hookDown('beforeEach', function (err, errSuite) {
  478. if (test.isPending()) {
  479. self.emit('pending', test);
  480. self.emit('test end', test);
  481. return next();
  482. }
  483. if (err) {
  484. return hookErr(err, errSuite, false);
  485. }
  486. self.currentRunnable = self.test;
  487. self.runTest(function (err) {
  488. test = self.test;
  489. if (err) {
  490. var retry = test.currentRetry();
  491. if (err instanceof Pending) {
  492. test.pending = true;
  493. self.emit('pending', test);
  494. } else if (retry < test.retries()) {
  495. var clonedTest = test.clone();
  496. clonedTest.currentRetry(retry + 1);
  497. tests.unshift(clonedTest);
  498. // Early return + hook trigger so that it doesn't
  499. // increment the count wrong
  500. return self.hookUp('afterEach', next);
  501. } else {
  502. self.fail(test, err);
  503. }
  504. self.emit('test end', test);
  505. if (err instanceof Pending) {
  506. return next();
  507. }
  508. return self.hookUp('afterEach', next);
  509. }
  510. test.state = 'passed';
  511. self.emit('pass', test);
  512. self.emit('test end', test);
  513. self.hookUp('afterEach', next);
  514. });
  515. });
  516. }
  517. this.next = next;
  518. this.hookErr = hookErr;
  519. next();
  520. };
  521. /**
  522. * Run the given `suite` and invoke the callback `fn()` when complete.
  523. *
  524. * @api private
  525. * @param {Suite} suite
  526. * @param {Function} fn
  527. */
  528. Runner.prototype.runSuite = function (suite, fn) {
  529. var i = 0;
  530. var self = this;
  531. var total = this.grepTotal(suite);
  532. var afterAllHookCalled = false;
  533. debug('run suite %s', suite.fullTitle());
  534. if (!total || (self.failures && suite._bail)) {
  535. return fn();
  536. }
  537. this.emit('suite', this.suite = suite);
  538. function next (errSuite) {
  539. if (errSuite) {
  540. // current suite failed on a hook from errSuite
  541. if (errSuite === suite) {
  542. // if errSuite is current suite
  543. // continue to the next sibling suite
  544. return done();
  545. }
  546. // errSuite is among the parents of current suite
  547. // stop execution of errSuite and all sub-suites
  548. return done(errSuite);
  549. }
  550. if (self._abort) {
  551. return done();
  552. }
  553. var curr = suite.suites[i++];
  554. if (!curr) {
  555. return done();
  556. }
  557. // Avoid grep neglecting large number of tests causing a
  558. // huge recursive loop and thus a maximum call stack error.
  559. // See comment in `this.runTests()` for more information.
  560. if (self._grep !== self._defaultGrep) {
  561. Runner.immediately(function () {
  562. self.runSuite(curr, next);
  563. });
  564. } else {
  565. self.runSuite(curr, next);
  566. }
  567. }
  568. function done (errSuite) {
  569. self.suite = suite;
  570. self.nextSuite = next;
  571. if (afterAllHookCalled) {
  572. fn(errSuite);
  573. } else {
  574. // mark that the afterAll block has been called once
  575. // and so can be skipped if there is an error in it.
  576. afterAllHookCalled = true;
  577. // remove reference to test
  578. delete self.test;
  579. self.hook('afterAll', function () {
  580. self.emit('suite end', suite);
  581. fn(errSuite);
  582. });
  583. }
  584. }
  585. this.nextSuite = next;
  586. this.hook('beforeAll', function (err) {
  587. if (err) {
  588. return done();
  589. }
  590. self.runTests(suite, next);
  591. });
  592. };
  593. /**
  594. * Handle uncaught exceptions.
  595. *
  596. * @param {Error} err
  597. * @api private
  598. */
  599. Runner.prototype.uncaught = function (err) {
  600. if (err) {
  601. debug('uncaught exception %s', err !== function () {
  602. return this;
  603. }.call(err) ? err : (err.message || err));
  604. } else {
  605. debug('uncaught undefined exception');
  606. err = undefinedError();
  607. }
  608. err.uncaught = true;
  609. var runnable = this.currentRunnable;
  610. if (!runnable) {
  611. runnable = new Runnable('Uncaught error outside test suite');
  612. runnable.parent = this.suite;
  613. if (this.started) {
  614. this.fail(runnable, err);
  615. } else {
  616. // Can't recover from this failure
  617. this.emit('start');
  618. this.fail(runnable, err);
  619. this.emit('end');
  620. }
  621. return;
  622. }
  623. runnable.clearTimeout();
  624. // Ignore errors if complete or pending
  625. if (runnable.state || runnable.isPending()) {
  626. return;
  627. }
  628. this.fail(runnable, err);
  629. // recover from test
  630. if (runnable.type === 'test') {
  631. this.emit('test end', runnable);
  632. this.hookUp('afterEach', this.next);
  633. return;
  634. }
  635. // recover from hooks
  636. if (runnable.type === 'hook') {
  637. var errSuite = this.suite;
  638. // if hook failure is in afterEach block
  639. if (runnable.fullTitle().indexOf('after each') > -1) {
  640. return this.hookErr(err, errSuite, true);
  641. }
  642. // if hook failure is in beforeEach block
  643. if (runnable.fullTitle().indexOf('before each') > -1) {
  644. return this.hookErr(err, errSuite, false);
  645. }
  646. // if hook failure is in after or before blocks
  647. return this.nextSuite(errSuite);
  648. }
  649. // bail
  650. this.emit('end');
  651. };
  652. /**
  653. * Cleans up the references to all the deferred functions
  654. * (before/after/beforeEach/afterEach) and tests of a Suite.
  655. * These must be deleted otherwise a memory leak can happen,
  656. * as those functions may reference variables from closures,
  657. * thus those variables can never be garbage collected as long
  658. * as the deferred functions exist.
  659. *
  660. * @param {Suite} suite
  661. */
  662. function cleanSuiteReferences (suite) {
  663. function cleanArrReferences (arr) {
  664. for (var i = 0; i < arr.length; i++) {
  665. delete arr[i].fn;
  666. }
  667. }
  668. if (isArray(suite._beforeAll)) {
  669. cleanArrReferences(suite._beforeAll);
  670. }
  671. if (isArray(suite._beforeEach)) {
  672. cleanArrReferences(suite._beforeEach);
  673. }
  674. if (isArray(suite._afterAll)) {
  675. cleanArrReferences(suite._afterAll);
  676. }
  677. if (isArray(suite._afterEach)) {
  678. cleanArrReferences(suite._afterEach);
  679. }
  680. for (var i = 0; i < suite.tests.length; i++) {
  681. delete suite.tests[i].fn;
  682. }
  683. }
  684. /**
  685. * Run the root suite and invoke `fn(failures)`
  686. * on completion.
  687. *
  688. * @param {Function} fn
  689. * @return {Runner} for chaining
  690. * @api public
  691. * @param {Function} fn
  692. * @return {Runner} Runner instance.
  693. */
  694. Runner.prototype.run = function (fn) {
  695. var self = this;
  696. var rootSuite = this.suite;
  697. // If there is an `only` filter
  698. if (this.hasOnly) {
  699. filterOnly(rootSuite);
  700. }
  701. fn = fn || function () {};
  702. function uncaught (err) {
  703. self.uncaught(err);
  704. }
  705. function start () {
  706. self.started = true;
  707. self.emit('start');
  708. self.runSuite(rootSuite, function () {
  709. debug('finished running');
  710. self.emit('end');
  711. });
  712. }
  713. debug('start');
  714. // references cleanup to avoid memory leaks
  715. this.on('suite end', cleanSuiteReferences);
  716. // callback
  717. this.on('end', function () {
  718. debug('end');
  719. process.removeListener('uncaughtException', uncaught);
  720. fn(self.failures);
  721. });
  722. // uncaught exception
  723. process.on('uncaughtException', uncaught);
  724. if (this._delay) {
  725. // for reporters, I guess.
  726. // might be nice to debounce some dots while we wait.
  727. this.emit('waiting', rootSuite);
  728. rootSuite.once('run', start);
  729. } else {
  730. start();
  731. }
  732. return this;
  733. };
  734. /**
  735. * Cleanly abort execution.
  736. *
  737. * @api public
  738. * @return {Runner} Runner instance.
  739. */
  740. Runner.prototype.abort = function () {
  741. debug('aborting');
  742. this._abort = true;
  743. return this;
  744. };
  745. /**
  746. * Filter suites based on `isOnly` logic.
  747. *
  748. * @param {Array} suite
  749. * @returns {Boolean}
  750. * @api private
  751. */
  752. function filterOnly (suite) {
  753. if (suite._onlyTests.length) {
  754. // If the suite contains `only` tests, run those and ignore any nested suites.
  755. suite.tests = suite._onlyTests;
  756. suite.suites = [];
  757. } else {
  758. // Otherwise, do not run any of the tests in this suite.
  759. suite.tests = [];
  760. utils.forEach(suite._onlySuites, function (onlySuite) {
  761. // If there are other `only` tests/suites nested in the current `only` suite, then filter that `only` suite.
  762. // Otherwise, all of the tests on this `only` suite should be run, so don't filter it.
  763. if (hasOnly(onlySuite)) {
  764. filterOnly(onlySuite);
  765. }
  766. });
  767. // Run the `only` suites, as well as any other suites that have `only` tests/suites as descendants.
  768. suite.suites = filter(suite.suites, function (childSuite) {
  769. return indexOf(suite._onlySuites, childSuite) !== -1 || filterOnly(childSuite);
  770. });
  771. }
  772. // Keep the suite only if there is something to run
  773. return suite.tests.length || suite.suites.length;
  774. }
  775. /**
  776. * Determines whether a suite has an `only` test or suite as a descendant.
  777. *
  778. * @param {Array} suite
  779. * @returns {Boolean}
  780. * @api private
  781. */
  782. function hasOnly (suite) {
  783. return suite._onlyTests.length || suite._onlySuites.length || some(suite.suites, hasOnly);
  784. }
  785. /**
  786. * Filter leaks with the given globals flagged as `ok`.
  787. *
  788. * @api private
  789. * @param {Array} ok
  790. * @param {Array} globals
  791. * @return {Array}
  792. */
  793. function filterLeaks (ok, globals) {
  794. return filter(globals, function (key) {
  795. // Firefox and Chrome exposes iframes as index inside the window object
  796. if (/^\d+/.test(key)) {
  797. return false;
  798. }
  799. // in firefox
  800. // if runner runs in an iframe, this iframe's window.getInterface method
  801. // not init at first it is assigned in some seconds
  802. if (global.navigator && (/^getInterface/).test(key)) {
  803. return false;
  804. }
  805. // an iframe could be approached by window[iframeIndex]
  806. // in ie6,7,8 and opera, iframeIndex is enumerable, this could cause leak
  807. if (global.navigator && (/^\d+/).test(key)) {
  808. return false;
  809. }
  810. // Opera and IE expose global variables for HTML element IDs (issue #243)
  811. if (/^mocha-/.test(key)) {
  812. return false;
  813. }
  814. var matched = filter(ok, function (ok) {
  815. if (~ok.indexOf('*')) {
  816. return key.indexOf(ok.split('*')[0]) === 0;
  817. }
  818. return key === ok;
  819. });
  820. return !matched.length && (!global.navigator || key !== 'onerror');
  821. });
  822. }
  823. /**
  824. * Array of globals dependent on the environment.
  825. *
  826. * @return {Array}
  827. * @api private
  828. */
  829. function extraGlobals () {
  830. if (typeof process === 'object' && typeof process.version === 'string') {
  831. var parts = process.version.split('.');
  832. var nodeVersion = utils.reduce(parts, function (a, v) {
  833. return a << 8 | v;
  834. });
  835. // 'errno' was renamed to process._errno in v0.9.11.
  836. if (nodeVersion < 0x00090B) {
  837. return ['errno'];
  838. }
  839. }
  840. return [];
  841. }