Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Added support for test-specific timeouts. Closes #134

TJ Holowaychukc823003c0ea4
diff --git a/lib/runnable.js b/lib/runnable.js
index 6c39c1c6..e94414df 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -46,6 +46,7 @@ Runnable.prototype.timeout = function(ms){
if (0 == arguments.length) return this._timeout;
debug('timeout %d', ms);
this._timeout = ms;
+ if (this.timer) this.resetTimeout();
return this;
};
@@ -71,6 +72,21 @@ Runnable.prototype.clearTimeout = function(){
clearTimeout(this.timer);
};
+/**
+ * Reset the timeout.
+ *
+ * @api private
+ */
+
+Runnable.prototype.resetTimeout = function(){
+ var self = this
+ , ms = this.timeout();
+ this.clearTimeout();
+ this.timer = setTimeout(function(){
+ self.callback(new Error('timeout of ' + ms + 'ms exceeded'));
+ }, ms);
+};
+
/**
* Run the test and invoke `fn(err)`.
*
@@ -108,6 +124,9 @@ Runnable.prototype.run = function(fn){
fn(err);
}
+ // for .resetTimeout()
+ this.callback = done;
+
// async
if (this.async) {
try {