javascriptunit-testingpromisemocha.jsexpect.js

How do I get a meaningful test error when assertion is in a promise?


I'm having a hard time getting meaningful failures in tests when I need to check things in a promise.

That's because most testing frameworks use throw when an assertion fails, but those are absorbed by the then of promises...

For example, in the following I'd like Mocha to tell me that 'hello' isn't equal to 'world'...

Promise.resolve(42).then(function() {
  "hello".should.equal("world") 
})

Complete fiddle here

With Mocha we can officially return the promise, but this swallows completely the error and is thus much worse...

Note: I'm using mocha and expect.js (as I want to be compatible with IE8)


Solution

  • With Mocha we can officially return the promise, but this swallows completely the error and is thus much worse...

    In your fiddle, you are using Mocha 1.9 which dates from April 2013, and did not support returning promises from tests. If I upgrade your fiddle to the latest Mocha, it works just fine.