1

I'm working to understand unit testing in JavaScript, using Mocha/Sinon/Chai. I have seen the function done() used. But I cannot seem to find documentation for this function. It does not seem to be a part of the JavaScript language. If it were, I would expect to see it in the Mozilla documentation under [something].prototype.done(). But it's not there. I don't see it under jQuery's documentation, nor under Mocha's.

On another thread, I see this example of done():

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

What is done(), what language or tooling is it a part of, and where is the documentation for it? Is done() just a naming convention for a callback function?

Super Jade
  • 5,609
  • 7
  • 39
  • 61
zumafra
  • 1,255
  • 2
  • 13
  • 19
  • 2
    That looks wrong. Yes, it’s a naming convention for a callback function… but the callback parameter is missing. `function (done) {`? – Ry- Aug 18 '19 at 02:25
  • Ah - I think I'm getting it now. This kind of is a duplicate question to this then - https://stackoverflow.com/questions/28656780/what-is-the-attribute-done-in-nodejs - Thanks! – zumafra Aug 18 '19 at 02:30
  • 2
    Not really. There's no relation between the two other than they used the same name. jQuery also has a `done` method on jqXHR for example. In your case since you are using `mocha` it's related to [this](https://mochajs.org/#asynchronous-code). Continue reading through the promise use case which is what `db.put` uses. – MinusFour Aug 18 '19 at 02:33

1 Answers1

1

Done is a callback that mocha will provided as the first parameter to a unit testing it block. It is usually needed when testing asynchronous code, as it can be call to notify mocha that the it block is completed. It is good practice to name the callback done. However, you can name it as you want. You can find its documentation here just hit ctrl + f on windows or + f on MAC, then enter done.

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

Copied the following from mocha website.

Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete. By adding a callback (usually named done) to it(), Mocha will know that it should wait for this function to be called to complete the test. This callback accepts both an Error instance (or subclass thereof) or a falsy value; anything else will cause a failed test

Morlo Mbakop
  • 3,518
  • 20
  • 21
  • Thanks for pointing me to that - that's interesting. But with respect, after reading that, I'm not sure that `done()` is "a mocha built in function" as you say. It looks like it is a naming convention (from your quote: "usually named done") for a callback that Mocha looks for. In other words, Mocha looks for the callback ("usually named done"). – zumafra Aug 18 '19 at 02:50
  • @zumafra, agreed – Morlo Mbakop Aug 18 '19 at 03:37