-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Misleading source info in "done() called multiple times" message #1066
Comments
I ran into the same issue. Will be nice mocha can fix this. |
+1 |
For me this turned out to be a library, sailsjs (based on express), that I was using calling its done callback multiple times. Originally, my after(function(done) {
app.lower(done);
}); I added in a check and arbitrary wait time: after(function(done) {
var done_called = false;
app.lower(function() {
if (!done_called) {
done_called = true;
setTimeout(function() {
sails.log.debug("inside app.lower, callback not called yet. calling.");
done();
}, 1000);
} else {
sails.log.debug("inside app.lower, callback already called.");
}
});
}); This is not perfect but it allows me to call done once and use the |
resolves #1066: generate better async trace if done() is called multiple times
Awesome! Thanks @boneskull |
…d multiple times
@gardner I used your fix for the same issue in relation to sailsjs and it has fixed my issue but is this correct or should I have done something else? Thanks. |
I just stumbled into the same issue and wrote a little wrapper-class for the done-function: /**
* Wrapper-Class for done-function
* @param {Function} fn
* @class {Done}
*/
function Done(fn) {
var self = this;
var called = false;
/**
*
* @param {*} params...
*/
this.trigger = function (params) {
if(called) {
console.warn("done has already been called");
console.trace();
return;
}
fn.apply(self, arguments);
called = true;
};
} Usage: it('should test', function (fn) {
var doneWrap = new Done(fn);
myAsyncFn(function () {
// ...
doneWrap.trigger();
});
}); |
@gardner I'm seeing the same behavior with SailsJS, do you have a bug you can reference? I can't seem to find one. |
There's a related mocha bug: #1066 I can't find a sailsjs bug. It looks like issues are closed without much investigation on that project. |
Still happening even today. |
@CodeJjang How so? Can you provide code? I want to know if this hasn't been completely fixed. Note that if you call |
@boneskull of course that if you call it multiple times then you should get the message.
|
@CodeJjang That setTimeout is evaluated immediately and not as part of the |
Yep, indeed my bad.
and worked. |
Using done and promise resolution in the same flow also seems wierd. You could just return your promise in your hook. That works equally well, without a |
The error message "done() called multiple times" will be associated with whatever part of the test suite that's currently running. This can lead to a lot of confusion for the uninitiated developer.
For example, consider the following suite:
Running
mocha done-message.js
will result in:Note how the "done() called multiple times" will be listed having the
beforeEach
hook as the point where it came from. This is of course wrong, thebeforeEach
simply happens to be the active code part when the seconddone()
call is being run. Additionally, the second test gets marked as a failure, even though it did nothing wrong!I don't know Mocha's internals, so I don't know whether this can be fixed easily at all. Some ideas:
done
callback to the tests each time, that has the source information as a closure variable?done
error message is not associated to any code part at all?done
can come from anywhere?Tested with Mocha 1.15.1. This issue may or may not be related to #990.
More examples:
This will cause the error to be associated to the second test.
This will result in no error at all, even though
done
gets called multiple times!The text was updated successfully, but these errors were encountered: