I want to run my test file alone - node test/unit/test_404.js
but also with the test suite - node test/unit/runner.js
to achieve that I added an if/else statement in each test file to check if the code is running directly with the node executable or if it's required by other file.
if it was run directly, I run my server before running the test.
if the file was required by the test suite runner, I just export the test so the runner will call it (module.exports = test).
Here are 2 version of one of the test files -
http://hastebin.com/fovofufome.js They are doing the same thing, except that in version 2 I moved the runServerBeforeTest function to it's own helper file.
I found the first version more readable but it's more verbose and I need to repeate that in each test file.
Any opinions or even different approaches for testing?
btw, I deliberately uses node's built-in asserts and not using any test framework. I would like to feel pain before adding libraries and so far I am happy with this 'leas' approach.
Pasting my code here as well:
// test_404.js version 1
// core modules
var assert = require('assert');
// npm package
var request = require('request');
// GET /not-exist should return 404 - not found
function test(config) {
console.log('GET /not-exist');
request({
uri: "http://localhost:" + config.webSitePort + "/not-exist",
followRedirect: false
},
function (err, res, body) {
if (err) {
console.log('Error in ' + __filename + '. ' + err);
} else {
assert.equal(res.statusCode, 404)
// process.exit();
};
});
};
// The following code let us run this test file by itself or as part of the runner.js
// if this file is by itself with 'node test_foo.js' we need to run the server before calling the test
// else - expose the test function since the runner.js already run the server
if (module === require.main) {
runServerBeforeTest();
} else {
module.exports = test;
};
function runServerBeforeTest() {
// my modules
var config = require('../../config/test.js');
var app = require('../../app.js');
app.init(config, function (err, msg) {
if (err) {
console.log('Error during application init: ' + err);
} else {
console.log(msg);
app.start(test(config));
}
});
};
// test_404.js version 2
// core modules
var assert = require('assert');
// npm package
var request = require('request');
// GET /not-exist should return 404 - not found
function test(config) {
console.log('GET /not-exist');
request({
uri: "http://localhost:" + config.webSitePort + "/not-exist",
followRedirect: false
},
function (err, res, body) {
if (err) {
console.log('Error in ' + __filename + '. ' + err);
} else {
assert.equal(res.statusCode, 404)
// process.exit();
};
});
};
// The following code let us run this test file by itself or as part of the runner.js
// if this file is by itself with 'node test_foo.js' we need to run the server before calling the test
// else - expose the test function since the runner.js already run the server
if (module === require.main) {
runServerBeforeTest();
require(__dirname + '/helper.js').runServerBeforeTest(test);
} else {
module.exports = test;
};
// helper.js - after my server is listening, run a given test
module.exports.runServerBeforeTest = runServerBeforeTest;
function runServerBeforeTest(test) {
// my modules
var config = require('../../config/test.js');
var app = require('../../app.js');
app.init(config, function (err, msg) {
if (err) {
console.log('Error during application init: ' + err);
} else {
console.log(msg);
app.start(test(config));
}
});
};
// runner.js - after my server is listening - run all test*.js files
// my modules
var config = require('../../config/test.js');
var app = require('../../app.js');
var helper = require(__dirname + '/helper.js');
function runTests() {
console.log('running tests:');
testFiles = [];
var test = null;
testFiles = require("fs").readdirSync(__dirname).filter(function(file) {
return (/^test/.test(file))
});
testFiles.forEach(function(file) {
test = require("./" + file);
if (typeof(test) === 'function') {
test(config);
}
});
};
app.init(config, function (err, msg) {
if (err) {
console.log('Error during application init: ' + err);
} else {
console.log(msg);
app.start(runTests(config));
}
});