Skip to content

armgit / mocha

Public Imported from GitHub
0

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

Fixed: clear timer on failure. Closes #80

Tj Holowaychuk7591ce6a3d1a
diff --git a/lib/runner.js b/lib/runner.js
index 589de1eb..3f929ff1 100644
--- a/lib/runner.js
+++ b/lib/runner.js
@@ -248,32 +248,17 @@ Runner.prototype.runTest = function(fn){
var test = this.test
, self = this;
- // async
- if (test.async) {
- try {
- test.run(function(err){
- if (test.finished) {
- self.fail(test, new Error('done() called multiple times'));
- return;
- }
- fn(err);
- });
- } catch (err) {
+ try {
+ test.run(function(err){
+ if (test.finished) {
+ if (!test.failed) self.fail(test, new Error('done() called multiple times'));
+ return;
+ }
fn(err);
- }
-
- return;
+ });
+ } catch (err) {
+ fn(err);
}
-
- // sync
- process.nextTick(function(){
- try {
- test.run();
- fn();
- } catch (err) {
- fn(err);
- }
- });
};
/**
diff --git a/lib/test.js b/lib/test.js
index 41301996..9e764f75 100644
--- a/lib/test.js
+++ b/lib/test.js
@@ -55,10 +55,10 @@ Test.prototype.fullTitle = function(){
*/
Test.prototype.run = function(fn){
- var timer
- , self = this
+ var self = this
, ms = this._timeout
- , start = new Date;
+ , start = new Date
+ , timer;
// timeout
if (this.async) {
@@ -69,15 +69,27 @@ Test.prototype.run = function(fn){
// async
if (this.async) {
- this.fn(function(err){
+ try {
+ this.fn(function(err){
+ clearTimeout(timer);
+ self.duration = new Date - start;
+ fn(err);
+ self.finished = true;
+ });
+ } catch (err) {
clearTimeout(timer);
- self.duration = new Date - start;
fn(err);
- self.finished = true;
- });
+ this.finished = true;
+ }
+ return;
+ }
+
// sync
- } else {
+ try {
if (!this.pending) this.fn();
this.duration = new Date - start;
+ fn();
+ } catch (err) {
+ fn(err);
}
};