Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added setZeroTimeout timeout for browser. Closes #153

TJ Holowaychuk0c72f1b6709f
diff --git a/Makefile b/Makefile
index 49c30016..b067a054 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,12 @@ mocha.css: test/browser/style.css
mocha.js: $(SRC)
@node support/compile $^
- @cat support/head.js _mocha.js support/{tail,foot}.js > mocha.js
+ @cat \
+ support/head.js \
+ _mocha.js \
+ support/setZeroTimeout.js \
+ support/{tail,foot}.js \
+ > mocha.js
clean:
rm -f mocha.{js,css}
diff --git a/mocha.js b/mocha.js
index 37864301..629d6909 100644
--- a/mocha.js
+++ b/mocha.js
@@ -1140,6 +1140,10 @@ function HTML(runner) {
stack.shift();
});
+ runner.on('fail', function(test, err){
+ runner.emit('test end', test);
+ });
+
runner.on('test end', function(test){
// TODO: add to stats
var percent = stats.tests / total * 100 | 0;
@@ -2699,7 +2703,47 @@ module.exports = function(paths, fn){
});
};
}); // module: watch.js
+// Only add setZeroTimeout to the window object, and hide everything else in a closure.
+(function() {
+
+ var timeouts = [],
+ messageName = 'zero-timeout-message';
+
+ // Like setTimeout, but only takes a function argument. There's
+ // no time argument (always zero) and no arguments (you have to
+ // use a closure).
+ function setZeroTimeoutPostMessage(fn) {
+ timeouts.push(fn);
+ window.postMessage(messageName, '*');
+ }
+
+ function setZeroTimeout(fn) {
+ setTimeout(fn, 0);
+ }
+
+ function handleMessage(event) {
+ if (event.source == window && event.data == messageName) {
+ if (event.stopPropagation) {
+ event.stopPropagation();
+ }
+ if (timeouts.length) {
+ timeouts.shift()();
+ }
+ }
+ }
+
+ if (window.postMessage) {
+ if (window.addEventListener) {
+ window.addEventListener('message', handleMessage, true);
+ } else if (window.attachEvent) {
+ window.attachEvent('onmessage', handleMessage);
+ }
+ window.setZeroTimeout = setZeroTimeoutPostMessage;
+ } else {
+ window.setZeroTimeout = setZeroTimeout;
+ }
+}());
/**
* Node shims.
*
@@ -2714,7 +2758,7 @@ process.exit = function(status){};
process.stdout = {};
global = this;
-process.nextTick = function(fn){ fn(); };
+process.nextTick = setZeroTimeout;
process.removeListener = function(ev){
if ('uncaughtException' == ev) {
diff --git a/support/setZeroTimeout.js b/support/setZeroTimeout.js
new file mode 100644
index 00000000..ac9384c1
--- /dev/null
+++ b/support/setZeroTimeout.js
@@ -0,0 +1,41 @@
+// Only add setZeroTimeout to the window object, and hide everything else in a closure.
+(function() {
+
+ var timeouts = [],
+ messageName = 'zero-timeout-message';
+
+ // Like setTimeout, but only takes a function argument. There's
+ // no time argument (always zero) and no arguments (you have to
+ // use a closure).
+ function setZeroTimeoutPostMessage(fn) {
+ timeouts.push(fn);
+ window.postMessage(messageName, '*');
+ }
+
+ function setZeroTimeout(fn) {
+ setTimeout(fn, 0);
+ }
+
+ function handleMessage(event) {
+ if (event.source == window && event.data == messageName) {
+ if (event.stopPropagation) {
+ event.stopPropagation();
+ }
+ if (timeouts.length) {
+ timeouts.shift()();
+ }
+ }
+ }
+
+ if (window.postMessage) {
+ if (window.addEventListener) {
+ window.addEventListener('message', handleMessage, true);
+ } else if (window.attachEvent) {
+ window.attachEvent('onmessage', handleMessage);
+ }
+ window.setZeroTimeout = setZeroTimeoutPostMessage;
+ } else {
+ window.setZeroTimeout = setZeroTimeout;
+ }
+
+}());
\ No newline at end of file
diff --git a/support/tail.js b/support/tail.js
index e11db765..f92420c2 100644
--- a/support/tail.js
+++ b/support/tail.js
@@ -13,7 +13,7 @@ process.exit = function(status){};
process.stdout = {};
global = this;
-process.nextTick = function(fn){ fn(); };
+process.nextTick = setZeroTimeout;
process.removeListener = function(ev){
if ('uncaughtException' == ev) {