w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How do I properly test promises with mocha and chai?

The easiest thing to do would be to use the built in promises support Mocha has in recent versions:

it('Should return the exchange rates for btc_ltc', function() {
// no done
    var pair = 'btc_ltc';
    // note the return
    return shapeshift.getRate(pair).then(function(data){
    });// no catch, it'll figure it out since the promise is rejected

Since this approach is promises end to end it is easier to test and you won't have to think about the strange cases you're thinking about like the odd done() calls everywhere.

This is an advantage Mocha has over other libraries like Jasmine at the moment. You might also want to check Chai As Promised which would make it even easier (no .then) but personally I prefer the clarity and simplicity of the current version

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.