5

If I write an asynchronous test using Mocha, all I need to do is to specify the done parameter on the test function:

test('foo', function (done) {...});

My question is: How does Mocha know whether done was given?

The definition of the test function should be something such as

function test(title, fn) {...};

How does Mocha check fn?

1 Answer 1

11

It uses the .length property on the test function.

To illustrate, try this in the Node REPL:

> (function() {}).length
0
> (function(done) {}).length
1

Here's the actual line in the source where this check happens:

this.async = fn && fn.length;
2
  • Great, thanks! I didn't know that such a property exists on functions :-)
    – Golo Roden
    Apr 7, 2013 at 20:58
  • No problem. That check happens on the fn reference.
    – jmar777
    Apr 7, 2013 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.