Saturday, May 11, 2024
121
rated 0 times [  128] [ 7]  / answers: 1 / hits: 24466  / 9 Years ago, thu, may 28, 2015, 12:00:00

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


More From » unit-testing

 Answers
82

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)

[#66426] Wednesday, May 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliano

Total Points: 381
Total Questions: 109
Total Answers: 93

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
emiliano questions
;