nodeunit icon indicating copy to clipboard operation
nodeunit copied to clipboard

Running the same test file multiple times ignores it’s setUp function

Open fsvehla opened this issue 14 years ago • 6 comments

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

fsvehla avatar Nov 16 '11 14:11 fsvehla

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?

zippo445 avatar Sep 27 '13 19:09 zippo445

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?

zippo445 avatar Sep 27 '13 19:09 zippo445

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?

s2young avatar Oct 04 '13 16:10 s2young

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

s2young avatar Oct 04 '13 16:10 s2young

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!

zippo445 avatar Oct 04 '13 16:10 zippo445

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;
};

pranavjha avatar Aug 07 '14 10:08 pranavjha