Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Mocha know that done was specified?

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?

like image 601
Golo Roden Avatar asked Dec 26 '22 06:12

Golo Roden


1 Answers

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;
like image 177
jmar777 Avatar answered Mar 03 '23 23:03

jmar777