Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Add promise support (close #329, close #1036)

Travis Jefferya2968bf64e13
diff --git a/lib/runnable.js b/lib/runnable.js
index 03b8792b..42e5c4c7 100644
--- a/lib/runnable.js
+++ b/lib/runnable.js
@@ -170,12 +170,12 @@ Runnable.prototype.run = function(fn){
// timeout
if (this.async) {
- if (ms) {
- this.timer = setTimeout(function(){
+ this.timer = setTimeout(function(){
+ if (!finished) {
done(new Error('timeout of ' + ms + 'ms exceeded'));
self.timedOut = true;
- }, ms);
- }
+ }
+ }, ms);
}
// called multiple times
@@ -218,10 +218,22 @@ Runnable.prototype.run = function(fn){
// sync
try {
- if (!this.pending) this.fn.call(ctx);
- this.duration = new Date - start;
- fn();
+ if (!this.pending) {
+ var result = this.fn.call(ctx);
+ if (result && typeof result.then === 'function') {
+ result.then(
+ function(){
+ done();
+ },
+ done
+ );
+ } else {
+ done();
+ }
+ } else {
+ done();
+ }
} catch (err) {
- fn(err);
+ done(err);
}
};