How does Mocha know that done was specified?

浪子不回头ぞ 提交于 2019-12-02 01:03:09

问题


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:


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;


来源:https://stackoverflow.com/questions/15867672/how-does-mocha-know-that-done-was-specified

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!