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);
Seems like a decent compromise for a clean setup.