3

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

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
Adron
  • 1,698
  • 3
  • 19
  • 40
  • 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 Jan 17 '14 at 01:16
  • possible duplicate of [Testing asynchronous function with mocha](http://stackoverflow.com/questions/12159846/testing-asynchronous-function-with-mocha) – Louis Jan 17 '14 at 12:58
  • @juanpaco yes, check out the answer below by notmyself. – Adron Mar 16 '14 at 02:51
  • if the tests executes "without waiting" doesn't mean it is asynchronous? – ovi Aug 03 '16 at 08:28

2 Answers2

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 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)
})
NotMyself
  • 29,209
  • 17
  • 56
  • 74
  • Tried both... I'm still not getting anything out of .then or .fail. :-/ – Adron Jan 17 '14 at 00:39
  • Added the full repo so you could give it a go. Put EVERYTHING in it. Including the Webstorm .idea directory. It's just a few bits of code. No idea what I'm actually doing here. :-/ – Adron Jan 17 '14 at 00:44
  • Oh wait, I see where ... oh man, my brain is twisted now. I'm gonna get this Javascript stuff yet. ;) The first time I'd tried it I missed the 'done' that is passed into the callback. It's kind of important! – Adron Jan 17 '14 at 03:04
  • what am I missing here? using done it makes it wait, does synchronous test, not asynchronous test. – ovi Aug 03 '16 at 08:29
2

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();
        })
})
Daniel
  • 38,041
  • 11
  • 92
  • 73
  • Fixed the (), it just got left in the code I pasted above. However that didn't seem to fix the problem. :( – Adron Jan 17 '14 at 00:38
  • Added the full repo so you could give it a go. Put EVERYTHING in it. Including the Webstorm .idea directory. It's just a few bits of code. No idea what I'm actually doing here. :-/ – Adron Jan 17 '14 at 00:52