Repository: mochajs/mocha

Problem statement:
Mocha.run throws a TypeError if callback is not provided
To reproduce the issue, install Mocha 2.1.0 and run this script on Node console.

``` javascript
var Mocha = require("mocha");
var mocha = new Mocha();
mocha.addFile("./test.js");
mocha.run();
```

Here is `test.js`

``` javascript
it("nothing", function() {});
```

Then, I get:

```
Donghwan@DONGHWAN ~/Documents/GitHub/vibe-javascript-client (master)
$ node test-runner


  √ nothing

  1 passing (7ms)


c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\mocha.js:414
          fn(failures);
          ^
TypeError: undefined is not a function
    at done (c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\mocha.js:414:11)
    at Runner.<anonymous> (c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\runner.js:590:5)
    at Runner.emit (events.js:117:20)
    at c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\runner.js:597:10
    at c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\runner.js:518:7
    at next (c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\runner.js:248:23)
    at Object._onImmediate (c:\Users\Donghwan\Documents\GitHub\vibe-javascript-client\node_modules\mocha\lib\runner.js:276:5)
    at processImmediate [as _immediateCallback] (timers.js:345:15)
```

2.0.1 works fine. As the [wiki page](https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically) shows use of `mocha.run()`, I think it's a regression in 2.1.0. A workaround is [to pass an empty callback](https://github.com/vibe-project/vibe-javascript-client/commit/058f9d5020306cf2800e9f2862b4a773637483c5).

It looks like that https://github.com/mochajs/mocha/commit/30582e646d579ce67ef4229922651da498e79a80 is the cause. Before executing `fn(failures);`, it should check if `fn` is available.



Hints:
Same issue here, the workaround solved it for now.

@dasilvacontin As @julien-f experienced the same issue, you may want to change the label, unconfirmed, to something like invalid or bug?

Sure, thanks for the heads up, @flowersinthesand!


Patch:
diff --git a/lib/mocha.js b/lib/mocha.js
--- a/lib/mocha.js
+++ b/lib/mocha.js
@@ -410,9 +410,7 @@ Mocha.prototype.run = function(fn){
   function done(failures) {
       if (reporter.done) {
           reporter.done(failures, fn);
-      } else {
-          fn(failures);
-      }
+      } else fn && fn(failures);
   }
 
   return runner.run(done);


Test patch:
diff --git a/test/mocha.js b/test/mocha.js
new file mode 100644
--- /dev/null
+++ b/test/mocha.js
@@ -0,0 +1,33 @@
+var Mocha = require('../');
+var Test = Mocha.Test;
+
+describe('Mocha', function(){
+  var blankOpts = { reporter: function(){} }; // no output
+
+  describe('.run(fn)', function(){
+    it('should not raise errors if callback was not provided', function(){
+      var mocha = new Mocha(blankOpts);
+      mocha.run();
+    })
+
+    it('should execute the callback when complete', function(done) {
+      var mocha = new Mocha(blankOpts);
+      mocha.run(function(){
+        done();
+      })
+    })
+
+    it('should execute the callback with the number of failures '+
+      'as parameter', function(done) {
+      var mocha = new Mocha(blankOpts);
+      var failingTest = new Test('failing test', function(){
+        throw new Error('such fail');
+      });
+      mocha.suite.addTest(failingTest);
+      mocha.run(function(failures) {
+        failures.should.equal(1);
+        done();
+      });
+    })
+  })
+})