javascript - How does Mocha know to be async? -


this question has answer here:

mocha can determine whether expect callback (done), or whether should run test in sync, solely based off of passing callback parameter.

// sync it("should amazing", function (){     expect(amazing).equals(true); });  // async it("should amazing, eventually", function (done) {   settimeout(function () {     expect(amazing).equals(true);     done();   }, 1000); }); 

i don't know how this. way can think literally parses function string, breaks down , determines if parameter passed.

is happens?

you wouldn't expect it, functions have length property! equal number of arguments function takes.

i'll edit answer add snippet out of mocha when find it, that's valid way can detect whether or not accepts argument. check length if it's one, decides whether or not test async or not.

edit: i found it. context, it inherits runnable.

function runnable(title, fn) {     // other properties...     this.async = fn && fn.length;     // other properties... } 

Comments