Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Run multiple immediates per browser tick

John Firebaugh7696558d65ec
diff --git a/lib/runner.js b/lib/runner.js
index 3607ce40..ea0c28f2 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -8,9 +8,7 @@ var EventEmitter = require('events').EventEmitter
, Test = require('./test')
, utils = require('./utils')
, filter = utils.filter
- , keys = utils.keys
- , noop = function(){}
- , immediately = global.setImmediate || process.nextTick;
+ , keys = utils.keys;
/**
* Non-enumerable globals.
@@ -62,6 +60,14 @@ function Runner(suite) {
this.globals(this.globalProps().concat(['errno']));
}
+/**
+ * Wrapper for setImmediate, process.nextTick, or browser polyfill.
+ *
+ * @param {Function} fn
+ * @api private
+ */
+Runner.immediately = global.setImmediate || process.nextTick;
+
/**
* Inherit from `EventEmitter.prototype`.
*/
@@ -243,7 +249,7 @@ Runner.prototype.hook = function(name, fn){
});
}
- immediately(function(){
+ Runner.immediately(function(){
next(0);
});
};
diff --git a/support/tail.js b/support/tail.js
index 0f9b64a4..7216b998 100644
--- a/support/tail.js
+++ b/support/tail.js
@@ -12,34 +12,6 @@ process.exit = function(status){};
process.stdout = {};
global = window;
-/**
- * next tick implementation.
- */
-
-process.nextTick = (function(){
- // postMessage behaves badly on IE8
- if (window.ActiveXObject || !window.postMessage) {
- return function(fn){ fn() };
- }
-
- // based on setZeroTimeout by David Baron
- // - http://dbaron.org/log/20100309-faster-timeouts
- var timeouts = []
- , name = 'mocha-zero-timeout'
-
- window.addEventListener('message', function(e){
- if (e.source == window && e.data == name) {
- if (e.stopPropagation) e.stopPropagation();
- if (timeouts.length) timeouts.shift()();
- }
- }, true);
-
- return function(fn){
- timeouts.push(fn);
- window.postMessage(name, '*');
- }
-})();
-
/**
* Remove uncaughtException listener.
*/
@@ -72,6 +44,31 @@ process.on = function(e, fn){
var Mocha = window.Mocha = require('mocha'),
mocha = window.mocha = new Mocha({ reporter: 'html' });
+ var immediateQueue = []
+ , immediateTimeout;
+
+ function timeslice() {
+ var immediateStart = new Date().getTime();
+ while (immediateQueue.length && (new Date().getTime() - immediateStart) < 100) {
+ immediateQueue.shift()();
+ }
+ if (immediateQueue.length) {
+ immediateTimeout = setTimeout(timeslice, 0);
+ } else {
+ immediateTimeout = null;
+ }
+ }
+
+ /**
+ * High-performance override of Runner.immediately.
+ */
+ Mocha.Runner.immediately = function(callback) {
+ immediateQueue.push(callback);
+ if (!immediateTimeout) {
+ immediateTimeout = setTimeout(timeslice, 0);
+ }
+ };
+
/**
* Override ui to ensure that the ui functions are initialized.
* Normally this would happen in Mocha.prototype.loadFiles.