知らなかったのでメモ。
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/length
$ node
> (function () {})
[Function]
> (function () {}).length
0
> (function (a) {}).length
1
> (function (a, b) {}).length
2これを使って、例えばMochaではrunnable.jsで
function Runnable(title, fn) { this.title = title; this.fn = fn; this.async = fn && fn.length; this.sync = ! this.async; this._timeout = 2000; }
という具合にテスト対象の関数の同期/非同期判別を行っていて、
同期的なテストは
it('hoge', function () { require('assert').ok(true); });
非同期的なテストは
it('hoge', function (done) { require('assert').ok(true); done(); });
のように書く、と区別される。
done引数を取っているものは非同期的なテストとみなされるので、done()が呼ばれないとテストが終了しない。