Skip to content

Instantly share code, notes, and snippets.

@tmcw
Last active June 13, 2016 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/9f135b40b98168759434 to your computer and use it in GitHub Desktop.
Save tmcw/9f135b40b98168759434 to your computer and use it in GitHub Desktop.
Setup & Teardown of Tests in node-tap or tape

The node-tap and tape modules are great for testing node modules. But they don't have built-in ideas of how you should do setup and teardown. So here's one approach.

  • Tests run serially. Take advantage of this, rather than doing any more advanced functional magic.
  • Use global variables.
var foo, bar;

function setup(t) {
    test('setup', function(t) {
        asyncSetupFunctionLikeCreatingADatabase(function() {
            bar = 'someresult';
            t.end();
        });
    });
}

function teardown(cb) {
    test('teardown', function(t) {
        syncTearDownStuff();
        asyncTearDownFunction() {
            t.end();
        });
    });
}

setup(test);
test('hello', function(t) {
    functionThatReliesOnSetup(bar);
    t.end();
});
teardown(test);
@knownasilya
Copy link

Seems like a decent compromise for a clean setup.

@AWinterman
Copy link

this won't work if your test is async.

@bentaber
Copy link

It will work if your tests are async, as test() function callbacks are executed in serial https://github.com/substack/tape#testname-opts-cb

@bySabi
Copy link

bySabi commented Jun 13, 2016

tape creator @substack propose a better aproach: https://gist.github.com/substack/9561717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment