-
-
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
Mocha does not reset 'this' on every test run #1062
Comments
ya this is expected. doesn't make much sense to put something on |
Wait, I don't get your reasoning. In other unit testing frameworks it's quite common to set an instance variable in the setup/beforeEach function, which is then used in the test function. Something like this:
Upon running every test function, a new TestSuite instance is created, so that all instance variables are empty at the beginning. Are you telling me not to do something like this in mocha? Are you telling me to keep all my state in local variables within the test function? Then how do I share data between the beforeEach and the test function? |
ya that works in mocha. you're saying two different things now. this is how you do in mocha what you have there
|
Well, consider the following use case:
This test suite creates two socket connections on every test, and calls the callback only after both socket connections have been created. Pretty simple stuff to test networking code. But when I run it I get a surprising result:
"done() called multiple times"? After some investigation, it turns out that the following check doesn't work properly:
The intention is to only continue if both sockets have been connected. However, because the "works 1" test's The same thing happens in "works 3". I totally did not expect this to happen. The first thing I did was running "works 2" individually... which doesn't trigger the problem because there are no leftover data from a previous test! It took me quite a while to debug this. None of the other unit test frameworks that I've worked with so far preserve instance variables. Why does mocha? What do you suggest me to do in this case? Right now I'm working around this by putting all my state in a At the very least, you should document this behavior. |
you can clean up your socks in the beforeEach or in an afterEach or those socks don't even need to be set on this. you could do this var net = require('net');
describe("foo", function() {
this.timeout(2000);
beforeEach(function() {
this.createSockets = function(callback) {
var self = this, _sock1, _sock2;
function maybeDone() {
if (_sock1 && _sock2) {
callback();
}
}
var sock1 = net.connect(80, 'www.chello.nl', function(err) {
_sock1 = sock1;
maybeDone();
});
var sock2 = net.connect(80, 'www.chello.nl', function() {
_sock2 = sock2;
maybeDone();
});
}
});
it("works 1", function(done) {
var self = this;
this.createSockets(function() {
// Two sockets connected!
done();
});
});
it("works 2", function(done) {
var self = this;
this.createSockets(function() {
// Two sockets connected!
done();
});
});
it("works 3", function(done) {
var self = this;
this.createSockets(function() {
// Two sockets connected!
done();
});
});
}); |
Pretty much all unit test frameworks I've used, allow me to run each test in an isolated environment. I was surprised to find out that this is not the case with mocha:
The second test prints "true". I expected "undefined".
Is this how mocha is supposed to work, or did I do something wrong? I cannot find any documentation on what the intended behavior is. If this is the intended behavior, how do I reset 'this'? Do I have to manually delete everything?
The text was updated successfully, but these errors were encountered: