Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha exists after the first test failure when it is used programmatically

Tags:

mocha.js

I am trying to run mocha programmatically. The code of runner is the following:

var Mocha = require("mocha");
var mocha = new Mocha();
mocha.reporter('list').ui('bdd').ignoreLeaks();
//Here the serv_paths variable is defined, -- this code snippet is skipped
// ...
serv_paths.forEach(function(file){
mocha.addFile(file);
});
var runner = mocha.run(function(){ });

The tests are very simple:

 var assert = require("assert");
    describe('True!', function(){
        it('true = true', function(){
        assert.equal(true, true);
        });
        it('true = false', function(){
            assert.equal(true, false);
        });
        it('true = false -- second', function(){
            assert.equal(true, false);
    });
        it('true = true', function(){
            assert.equal(true, true);
        });
    });

The mocha runs tests before the first failure and then exits and ignore other tests.

The output is:

  [Jan 21 04:57:23]   ✓ True! true = true: 0ms
    [Jan 21 04:57:23]   1) True! true = false
    [Jan 21 04:57:23]   ✖ 1 of 4 tests failed:
    [Jan 21 04:57:23]   1) True! true = false:
     AssertionError: true == false
          at Context.<anonymous> (/home/ubuntu/project/plugins-server/cloud9.ide.help/test_test.js:14:10)
          at Test.run (/home/ubuntu/project/node_modules/mocha/lib/runnable.js:213:32)
          at Runner.runTest (/home/ubuntu/project/node_modules/mocha/lib/runner.js:343:10)
          at /home/ubuntu/project/node_modules/mocha/lib/runner.js:389:12
          at next (/home/ubuntu/project/node_modules/mocha/lib/runner.js:269:14)
          at /home/ubuntu/project/node_modules/mocha/lib/runner.js:278:7
          at next (/home/ubuntu/project/node_modules/mocha/lib/runner.js:226:23)
          at Array.<anonymous> (/home/ubuntu/project/node_modules/mocha/lib/runner.js:246:5)
          at EventEmitter._tickCallback (node.js:190:38)

Is there way to force Mocha run all tests?

like image 339
user1548340 Avatar asked Feb 17 '23 19:02

user1548340


1 Answers

After digging around in the sourcecode of Mocha i have found that the following works (in our situation). We appended the parameter 'bail' to our setup of the mocha suite

test.html

<script src="mocha.js"></script>
<script>
  mocha.ui('bdd');
  mocha.bail(false);//<-- this line is the magic
  mocha.reporter('html');
</script>

By appending the bail parameter it does not stop execution after the first failure. By default it seems this parameter is false (when running from the commandline) but true when executing tests via phantomjs. (We used Mocha v1.8.1)

like image 59
Mark van Straten Avatar answered Apr 30 '23 06:04

Mark van Straten