Running the same test file multiple times ignores it’s setUp function
When passing the same file multiple times to the nodeunit executable, the tests indeed run multiple times, but the setUp function is just called the first time.
Demonstration: https://github.com/fsvehla/nodeunit-bug-multiple-runs
have the same issue here. since I'm calling nodeunit to generate a dynamic test report of the code on the server, the setup and teardown are only called the first time the page is generated. has anyone looked into this?
it is caused by the delete group.setUp and delete group.tearDown in the following lines of core.js
if (group.setUp) {
setUps.push(group.setUp);
delete group.setUp;
}
if (group.tearDown) {
tearDowns.unshift(group.tearDown);
delete group.tearDown;
}
does anyone knows why the function need to be removed from the module?
I'm having this issue too because I wanted to run tests within a daemonized node script. Is there any way to flush out the run 'history' so the runner restarts from scratch each time I call reporter.run?
Hey guys, I don't know if this will help you but it works for me. I'm calling reporter.run within a single script multiple times on a timer. With this hack, the setUp and tearDown methods work every time.
https://github.com/s2young/nodeunit/commit/4dac1946d8485f16b7dfb13b8b60671e5aba6d25
I was able to fix the above problem by modifying core.js but then decided to leave that aside. after discussion with my team, we decided to switch our unit tests to jasmine-node and chai, a combination that seems more mature. We already have a lot on our plate coding and fixing bugs in our own stuff, so we'll leave development of test tools to others :) good luck!
adding setup and teardown in the object after the tests array is created fixes the issue
var wrapGroup = function (group, setUps, tearDowns) {
var tests = {};
var setUps = setUps ? setUps.slice(): [];
var tearDowns = tearDowns ? tearDowns.slice(): [];
var su = group.setUp;
var td = group.tearDown;
if (group.setUp) {
setUps.push(group.setUp);
delete group.setUp;
}
if (group.tearDown) {
tearDowns.unshift(group.tearDown);
delete group.tearDown;
}
var keys = _keys(group);
for (var i = 0; i < keys.length; i += 1) {
var k = keys[i];
if (typeof group[k] === 'function') {
tests[k] = wrapTest(
getSerialCallback(setUps),
getSerialCallback(tearDowns),
group[k]
);
}
else if (typeof group[k] === 'object') {
tests[k] = wrapGroup(group[k], setUps, tearDowns);
}
}
group.setUp = su;
group.tearDown = td;
return tests;
};