Error AssertionError Mocha

I run unit tests using Mocha, and instead of showing all thrown AssertionErrors in the reporter, Mocha crashes on the first error. Any suggestions?

Error on crash:

/Users/Robert/Code/JRJ/Server/node_modules/chai/lib/chai/assertion.js:106 throw new AssertionError(msg, { ^ AssertionError: expected 200 to equal 202 npm ERR! weird error 8 npm ERR! not ok code 0 

This is the same if I use Chai or the assert built-in library. I start Mocha with this command (I start it with npm test ):

 mocha --reporter 'spec' --recursive 

And the library versions used are the following:

  • node: 0.10.18
  • mocha: 1.12.0
  • chai: 1.8.0
  • hapi: 1.10.0

Security Code:

  var hapi = require('hapi'), expect = require('chai').expect, assert = require('assert'); describe("Customer API", function(){ var server = require('../../../../src/apis/customer'); //works as expected describe('simpleExample', function(){ it("should cause a test failure", function(done){ expect(200).to.equal(202); done(); }); }); //crashes Mocha describe('Authentication', function(){ it('Should get user token', function(done){ server.inject("/ auth?username=test@test.com &password=testa", function(res){ expect(res.statusCode).to.equal(202); //returns 200, crashes Mocha (the expected 202 is intentional to cause an assertion error) //assert.ok(res.statusCode === 202); expect(res.payload).to.be.a('string'); expect(res.payload).to.have.length(16); done(); }); }); }); }); 
+4
source share
1 answer

This is because it works the way urine works. Exceptions in asynchronous calls must be caught and passed to the completed callback, this even includes AssertionErrors. There is a bug in the Mocha documentation, and I solved the GitHub problem to solve this problem ( https://github.com/visionmedia/mocha/issues/982 ).

+3
source

All Articles