17

In JUnit (Java) the result of a unit test is either a succes, failure or error.

When i try to run a test with Mocha i either get a succes or assertion error.

Is is normally to get an AssertionError for failure tests? (shouldn't it just be called an failure and not an error?)

AssertionError: -1 == 2 + expected - actual

What about testing asynchronous code? When my tests fail i get an Uncaught eror? Is that normal?

Like this:

Uncaught Error: expected 200 to equal 201

user3452075
  • 411
  • 1
  • 6
  • 17

1 Answers1

21

What you are describing is the normal behavior for Mocha. This code illustrates what happens if you do not try to trap exceptions in asynchronous code (even raised by assertion failures) and what you can do if you want to avoid the uncaught exception message:

var assert = require("assert");

it("fails with uncaught exception", function (done) {
    setTimeout(function () {
        assert.equal(1, 2);
        done();
    }, 1000);
});

it("fails with assertion error", function (done) {
    setTimeout(function () {
        try {
            assert.equal(1, 2);
            done();
        }
        catch (e) {
            done(e);
        }
    }, 1000);
});

The code above produces this output:

  1) fails
  2) fails

  0 passing (2s)
  2 failing

  1)  fails:
     Uncaught AssertionError: 1 == 2
      at null._onTimeout (/tmp/t2/test.js:5:16)
      at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

  2)  fails:
     AssertionError: 1 == 2
      at null._onTimeout (/tmp/t2/test.js:13:20)
      at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Louis
  • 146,715
  • 28
  • 274
  • 320
  • You are totally right! I just want to remark that Mocha works with the assertion library by failing them when an exception is raised (i was not aware of that). – user3452075 May 28 '15 at 23:34