Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Honoring timeout for sync operations.

jsdevel6f912b8d5534
diff --git a/Makefile b/Makefile
index 6c555012..e18484fd 100644
--- a/Makefile
+++ b/Makefile
@@ -32,7 +32,7 @@ lib-cov:
test: test-unit
-test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-sort test-glob test-requires test-reporters test-only
+test-all: test-bdd test-tdd test-qunit test-exports test-unit test-grep test-jsapi test-compilers test-sort test-glob test-requires test-reporters test-only test-failing
test-jsapi:
@node test/jsapi
@@ -44,6 +44,17 @@ test-unit:
--growl \
test/*.js
+test-failing:
+ @./bin/mocha \
+ --reporter $(REPORTER) \
+ test/acceptance/failing/timeout.js > /dev/null 2>&1 ; \
+ failures="$$?" ; \
+ if [ "$$failures" != '2' ] ; then \
+ echo 'test-failing:' ; \
+ echo " expected 2 failing tests but saw $$failures" ; \
+ exit 1 ; \
+ fi
+
test-compilers:
@./bin/mocha \
--reporter $(REPORTER) \
@@ -165,4 +176,4 @@ non-tty:
tm:
@open editors/$(TM_BUNDLE)
-.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep tm clean
+.PHONY: test-cov test-jsapi test-compilers watch test test-all test-bdd test-tdd test-qunit test-exports test-unit non-tty test-grep test-failing tm clean
diff --git a/lib/runnable.js b/lib/runnable.js
index 6940bb77..c27eeed9 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -160,7 +160,6 @@ Runnable.prototype.globals = function(arr){
Runnable.prototype.run = function(fn){
var self = this
- , ms = this.timeout()
, start = new Date
, ctx = this.ctx
, finished
@@ -177,11 +176,13 @@ Runnable.prototype.run = function(fn){
// finished
function done(err) {
+ var ms = self.timeout();
if (self.timedOut) return;
if (finished) return multiple(err);
self.clearTimeout();
self.duration = new Date - start;
finished = true;
+ if (!err && self.duration > ms) err = new Error('timeout of ' + ms + 'ms exceeded');
fn(err);
}
diff --git a/test/acceptance/failing/timeout.js b/test/acceptance/failing/timeout.js
new file mode 100644
index 00000000..e92eef25
--- /dev/null
+++ b/test/acceptance/failing/timeout.js
@@ -0,0 +1,18 @@
+
+describe('timeout', function(){
+ this.timeout(20);
+
+ it('should be honored with sync suites', function(){
+ sleep(30);
+ });
+
+ it('should be honored with async suites', function(done){
+ sleep(30);
+ done();
+ });
+
+ function sleep(ms){
+ var start = Date.now();
+ while(start + ms > Date.now())continue;
+ }
+});