Mocha: I have asserts and sometimes errors in promises, but all tests passing! Why asserts in promises does not count as tests?

Created on 27 Oct 2016  路  4Comments  路  Source: mochajs/mocha

Using mocha for typescript tests

image

Errors like field is undefined (runtime error), or assert is failed.

faq

Most helpful comment

Asserts work by throwing exceptions; Mocha doesn't actually know about the asserts, it just catches the exceptions and uses their info for the test failure. Exceptions inside a promise turn into promise rejections. Mocha can't know about those if you're not returning the promise -- in fact, it can't even know to wait for the promise code to complete in that case. But if you return the promise Mocha will wait for it and detect those rejections as test failures.

The relevant section in the documentation is here, for what it's worth: https://mochajs.org/#working-with-promises Although you don't generally need to know about the done callback if you're using promises, as returning the promise is usually the simpler alternative.

All 4 comments

I think adding a code example is the first thing people will ask for

Asserts work by throwing exceptions; Mocha doesn't actually know about the asserts, it just catches the exceptions and uses their info for the test failure. Exceptions inside a promise turn into promise rejections. Mocha can't know about those if you're not returning the promise -- in fact, it can't even know to wait for the promise code to complete in that case. But if you return the promise Mocha will wait for it and detect those rejections as test failures.

The relevant section in the documentation is here, for what it's worth: https://mochajs.org/#working-with-promises Although you don't generally need to know about the done callback if you're using promises, as returning the promise is usually the simpler alternative.

It sometimes feels as if enforcing the usage of done when a Promise is not returned would help people.

I'm going to start labeling common mistakes, so we can see the bigger picture whenever we need to.

Yep, returning promise it 'it' do the trick! Mocha detects all fails in then() and test failes! Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

niftylettuce picture niftylettuce  路  3Comments

delta62 picture delta62  路  3Comments

eschwartz picture eschwartz  路  3Comments

3p3r picture 3p3r  路  3Comments

luoxi001713 picture luoxi001713  路  3Comments