I am a relative newbie to all things javascript, node.js, mocha etc.
In my code I have a Unit object that has a disable()
that sets the disabled property to true and a isDisabled()
that returns the disabled property. It also has a method nextTurnReset()
that resets the unit on the start of the next turn. I have written a test suite to test this behavior. I first disable the object and then try to test to see if it is disabled. However, the unit variable inside my first test - which is within the anonymous function passed to the Mocha's it()
method - is in the the non-disabled state as I observed with node's debugger.
describe('#disable()', function() {
var unit = tests.newUnit();
unit.disable();
debugger;
it('disabled off turn?', function() {
debugger;
(unit.isDisabled()).should.be.exactly(true);
});
unit.nextTurnReset();
it('disabled on next turn?', function() {
(unit.isDisabled()).should.be.exactly(true);
});
unit.nextTurnReset();
it('disabled on 2nd turn?', function() {
(unit.isDisabled()).should.be.exactly(false);
});
});
for the record, the first two tests fail, and the last one succeeds indicating the unit is never disabled at all.
from using the node debugger's repl: After the first debugger;
statement, unit.disabled == true
, but after the second debugger;
statement unit.disabled == false
. I expect the value to true in both cases.
Any idea why this would be the case? Also, what is the correct way of writing Mocha tests to get my expected result?
Thanks so much!
Passing state between examples is not a very good practice. What happens if you need to run tests in the random order? Or somebody in the project decides to move examples around?
For me having the following two examples seems to be enough to test Unit#disable properly
Variable scoping in Mocha is exactly the same as in any other JavaScript code. Your problem is that you do not realize in which order Mocha will execute your code. This is what happens:
You create your unit instance and call
disable
on it.You register your first test. That's what
it
does: it registers a test for future execution. The test does not execute now.You call
unit.nextTurnReset();
which resets your object's state.You register your second test. Again it does not execute now.
You reset your object again.
You register your last test.
After this, Mocha takes the tests you registered and runs them. By the time your tests are running your object is in its reset state, not disabled.
It seems to me that given the desired behavior you describe, your code should be:
The code passed to
beforeEach
is run before each test you've registered so it resets the object at the right time.