Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionError crashes Mocha

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

The error I get at crash is this:

/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

It's the same no matter if I use Chai or the built-in assert library. I run Mocha with this command (I run it with npm test):

mocha --reporter 'spec' --recursive

And the library versions I'm using are these:

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

The test 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("/[email protected]&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();
          });
        });
      });
    });
like image 342
robertherber Avatar asked Sep 23 '13 14:09

robertherber


People also ask

How do I stop mocha testing?

Find out how you can ignore some tests in Mocha You can use the skip() method (on describe() or it() ) to ignore some tests: In case of describe , it will ignore all tests in the describe block (which includes all nested describe and it functions); In case of it , it will ignore the individual test.

Is Mocha an assertion library?

Mocha is one such library. You can test one or more methods in your JavaScript code with Mocha. Assertions are a key part of writing automated tests with a tool like Mocha. They do the job of verifying that a piece of code returns the expected result.

Is Mocha a testing framework?

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.


1 Answers

This is because it's the way Mocha works. Exceptions in asynchronous calls needs to be caught and passed to the done callback, this even includes AssertionErrors. There is an error in the Mocha documentation and I've opened a GitHub issue to resolve this (https://github.com/visionmedia/mocha/issues/982).

like image 177
robertherber Avatar answered Sep 29 '22 16:09

robertherber