javascript - How do I use mocha to do asynchronous tests using 'done();'? - Stack Overflow

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

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:

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./Adron/node_testing_testing

Share Improve this question edited Jan 17, 2014 at 5:50 Has AlTaiar 4,1622 gold badges39 silver badges37 bronze badges asked Jan 16, 2014 at 23:34 AdronAdron 1,8763 gold badges21 silver badges41 bronze badges 4
  • Correct me if I'm wrong, but doesn't the function you pass to it call need to have the done parameter? it('should have data.', function(done) { vs. it('should have data.', function() {. – juanpaco Commented Jan 17, 2014 at 1:16
  • possible duplicate of Testing asynchronous function with mocha – Louis Commented Jan 17, 2014 at 12:58
  • @juanpaco yes, check out the answer below by notmyself. – Adron Commented Mar 16, 2014 at 2:51
  • if the tests executes "without waiting" doesn't mean it is asynchronous? – ovi Commented Aug 3, 2016 at 8:28
Add a ment  | 

2 Answers 2

Reset to default 5

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 plete 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)
})

I think you mean to actually call the done() callback.

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();
        })
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744891135a4599419.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信