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

powmedia opened this Issue Nov 17, 2011 · 5 comments


None yet

5 participants


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.


What workaround do you use to run setUp just once?


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


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;

exports['tests'] = {

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

    'test 1': function(test) {

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.


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