Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Open
powmedia opened this Issue · 5 comments

5 participants

Charles Davison mladenst Raynos (Jake Verbaten) Bryan Donovan Jay Kumar
Charles Davison

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.

mladenst

What workaround do you use to run setUp just once?

Raynos (Jake Verbaten)

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

Charles Davison

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) {
        ...
    }
}
Bryan Donovan

I've added a beforeAll in a fork: BryanDonovan@cd9718f

... 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.

Jay Kumar

Would like to see this feature merged into node unit soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.