Mocha test does not work with AssertionError

In JUnit (Java), the unit test result is either successful, or unsuccessful, or an error.

When I try to run a test using Mocha i, either get a succes or assertion error.

Do you usually get an AssertionError for failure tests? (shouldn't it just be called a failure, not a mistake?)

AssertionError: -1 == 2 + expected - actual

What about testing asynchronous code? When my tests fail, do I get Uncaught eror? This is normal?

Like this:

Search error: expected 200 to 201

+6
source share
1 answer

What you are describing is normal behavior for mocha. This code illustrates what happens if you don’t try to catch exceptions in asynchronous code (even if statement failures) and what you can do if you want to avoid the exception 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 above code 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) 
+10
source

Source: https://habr.com/ru/post/988091/


All Articles