Hey there,
I have a unit test script which can be seen here: https://gist.github.com/3bc91203fb434a19e9e4
The problem is that tests return the following:
ReferenceError: done is not defined
at Request._callback (/Users/john/Sites/symble/Backend/test/routes.js:43:4)
at Request.callback (/Users/john/Sites/symble/Backend/test/node_modules/request/main.js:108:22)
at Request.<anonymous> (/Users/john/Sites/symble/Backend/test/node_modules/request/main.js:468:18)
at Request.emit (events.js:67:17)
at IncomingMessage.<anonymous> (/Users/john/Sites/symble/Backend/test/node_modules/request/main.js:429:16)
at IncomingMessage.emit (events.js:88:20)
at HTTPParser.onMessageComplete (http.js:137:23)
at Socket.ondata (http.js:1137:24)
at TCP.onread (net.js:354:27)
It seems to happen randomly - on different tests each time. It always happens twice (ie: I get two of the above errors on two tests).
Look over your code again. Line 43 in the gist... in that method, "done" really isn't defined. It has no "function(done)" but rather "function()". That is, add the parameter 'done' to line 36 as you have in line 22.
Oops, sorry! Looks like I'm a turnip. Thanks @jprichardson :)
can i call the done method in a loop?
var list = {"a": 1,"b":2,"c":3};
for(var i in list) {
it('should test 【' + list[i] +'】', (function(type) {
console.log(type);
save(done);
})(i);
}
It report error with done is not defined.
@callblueday The function signature for the second argument of it is function (done) { ... }. If you want to do what you are currently doing you need to return a function, since what you are actually doing is an iife
@Munter Thanks very much! I change my code as follow
var list = {"a": 1,"b":2,"c":3};
for(var i in list) {
(doIt)(i);
}
function doIt(type) {
it('should test 【' + list[type] +'】', function(done) {
console.log(type);
save(done);
});
}
it works, but i do not understand why, can you explain it with more detail?
executing your first setup would execute the inlined iife immediately and likely return undefined, making the test always pass immediately. tests need a callback function to call when the test should be run, which is what you are doing in your last example.
Thanks again @Munter , that's very kind of you.
Most helpful comment
Look over your code again. Line 43 in the gist... in that method, "done" really isn't defined. It has no "function(done)" but rather "function()". That is, add the parameter 'done' to line 36 as you have in line 22.