Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done() is not defined #232

Closed
johnhamelink opened this issue Jan 23, 2012 · 7 comments
Closed

done() is not defined #232

johnhamelink opened this issue Jan 23, 2012 · 7 comments

Comments

@johnhamelink
Copy link

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).

@jprichardson
Copy link

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.

@johnhamelink
Copy link
Author

Oops, sorry! Looks like I'm a turnip. Thanks @jprichardson :)

kkaefer pushed a commit to kkaefer/mocha that referenced this issue Mar 30, 2012
kinda lame but this should suffice
@callblueday
Copy link

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.

@Munter
Copy link
Member

Munter commented Sep 21, 2016

@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

@callblueday
Copy link

callblueday commented Sep 21, 2016

@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?

@Munter
Copy link
Member

Munter commented Sep 21, 2016

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.

@callblueday
Copy link

Thanks again @Munter , that's very kind of you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants