Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use mocha to do asynchronous tests using 'done();'?

I'm working on trying to write an asynchronous test with mocha using the done(); call. This is my code so far.

it('should have data.', function () {
    db.put(collection, key, json_payload)
        .then(function (result) {
            result.should.exist;
            done();
        })
        .fail(function (err) {
            err.should.not.exist;
            done();
        })
})

The result however is that the code just executes without waiting for the then or fail to actually return with a result. Does done(); need to be at a different place within the code?

Also posted the whole repo right here: https://github.com/Adron/node_testing_testing

like image 344
Adron Avatar asked Mar 12 '26 22:03

Adron


1 Answers

if you want an async test you need to handle the done parameter

it('should have data.', function (done) {
    db.put(collection, key, json_payload)
        .then(function (result) {
            result.should.exist;
            done();
        })
        .fail(function (err) {
            err.should.not.exist;
            done();
        })
})

also if you are using Q as your promise library you might want to complete your chain like so.

it('should have data.', function (done) {
    db.put(collection, key, json_payload)
        .then(function (result) {
            result.should.exist;
        })
        .fail(function (err) {
            err.should.not.exist;

        })
        .done(done,done)
})
like image 171
NotMyself Avatar answered Mar 14 '26 11:03

NotMyself



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!