Reputation: 7496
When writing automated system/integration tests, it's common for the first step to run before all of the tests to be "start the server." Since starting a server can be expensive, it is desirable to do this one time, not before each individual test. JUnit has easy functionality for doing this. Is there an equivalent standard pattern in nodeunit? Or does it require hand rolling something?
Upvotes: 2
Views: 2343
Reputation: 1603
There are two ways to do this:
All tests in a nodeunit test file are run sequentially and synchronously. You could put the setup code for that group of tests in the first test, then the teardown in the last test.
If you want to do this more formally, and if you don't want to set up Grunt for a unit test, there's also a module called "nodeunit-async" that will let you run a global setup and teardown before and after all of your tests. You can run a global setup and teardown once before and after a group of tests.
Here's the blurb for nodeunit-async:
Lightweight wrapper for running asynchronous nodeunit tests. Particularly useful for when you want common global setup or teardown functions run for each test across multiple files, and/or fixture setup or teardown functions run once before and after all tests.
Designed for unit tests written using async's auto and waterfall methods.
https://github.com/Zugwalt/nodeunit-async
Upvotes: 0
Reputation: 344
Because your test suites using nodeunit are just node modules, you can take advantage of that closure for global setup/teardown (for that test suite only):
var myServer = require('./myservermodule');
var testsRun = 0;
var testsExpected = 3;
function startTest(test) {
test._reallyDone = test.done;
test.done = function() {
++testsRun;
test._reallyDone();
};
}
module.exports = {
'setUp' : function(cb) {
if (!myServer.server) myServer.start(cb);
else cb();
},
'tearDown' : function(cb) {
console.log("Tests run: " + testsRun + "/" + testsExpected);
if (testsRun===testsExpected) myServer.stop(cb);
else cb();
},
'sometest1' : function(test) {
startTest(test);
test.expect(1);
test.ok(true);
test.done();
},
'sometest2' : function(test) {
startTest(test);
test.expect(1);
test.ok(false);
test.done();
},
'sometest3' : function(test) {
startTest(test);
test.expect(1);
test.ok(true);
test.done();
}
};
Upvotes: 2
Reputation: 1436
Yes, Nodeunit has a setUp()
and a tearDown()
function that are always run before and after tests. You can use the setUp()
to start your server like this:
var server = require("path/to/server.js");
exports.setUp = function(callback) {
server.start(8080, function() {
callback();
});
};
// run your tests here, setUp will be called before each of them
This assumes in server.js you have:
exports.start = function() {
// start server here
}
The tearDown()
function runs after test.done()
is called.
For an example of this, check it out in action here: https://github.com/jamesshore/Lessons-Learned-Integration-Testing-Node/blob/master/src/_server_test.js
The documentation is here: https://github.com/caolan/nodeunit#groups-setup-and-teardown
Upvotes: 1
Reputation: 163548
I don't think Nodeunit has this built-in, but many folks handle such tasks with Grunt.
Upvotes: 2