nodeunit icon indicating copy to clipboard operation
nodeunit copied to clipboard

Add an init() method which runs once before a testcase

Open powmedia opened this issue 14 years ago • 5 comments

It's great having setUp() but quite often (e.g. when loading fixtures into a database) you don't want to do this before every test; you just want to do it once before any of the tests run. Adding fixtures before every test is much slower.

There are workarounds and ways to get setUp to run once but it would be great if there was a method (e.g. init()) which worked in this way.

powmedia avatar Nov 17 '11 00:11 powmedia

What workaround do you use to run setUp just once?

mladenst avatar Nov 19 '11 18:11 mladenst

+1 for .init() and +1 for what's the work around?

Raynos avatar Nov 20 '11 13:11 Raynos

The easiest way is probably just to use the first test to do all the setup, but I tend to wrap the setUp in a runOnce() function:

function runOnce(fn) {
    var hasRun = false;

    return function(done) {
        if (hasRun) {
            return done();
        }
        else {
            hasRun = true;
            fn(done);
        }
    }
},

exports['tests'] = {

    setUp: runOnce(function(done) {
        loadFixtures(function() {
            //Other setup
            done();
        });
    }),

    'test 1': function(test) {
        ...
    }
}

powmedia avatar Nov 20 '11 15:11 powmedia

I've added a beforeAll in a fork: https://github.com/BryanDonovan/nodeunit/commit/cd9718f9e7c0520757797322f8189a85b1ea739a

... but I'm not sure if it's the best way to go about it, and I haven't added an equivalent 'afterAll' method yet.

Let me know what you guys think and I'll finish it up and submit a pull request.

BryanDonovan avatar Nov 27 '11 22:11 BryanDonovan

Would like to see this feature merged into node unit soon

jayyvis avatar Aug 15 '12 14:08 jayyvis