简体   繁体   中英

Mocha Test Fails with AssertionError

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM