Skip to content

Instantly share code, notes, and snippets.

@nornagon
Created February 21, 2011 03:15
Show Gist options
  • Save nornagon/836616 to your computer and use it in GitHub Desktop.
Save nornagon/836616 to your computer and use it in GitHub Desktop.
Run node.js tests, each in their own node process.
var assert = require('assert')
, path = require('path')
function some_helper(foo) {
assert.equal(foo, 4)
}
module.exports = {
'test array length': function (test) {
some_helper([1,2,3,4].length)
test.finish()
},
'test async stuff': function (test) {
path.exists('/etc/passwd', function (exists) {
assert.ok(exists)
test.finish()
})
}
}
var fs = require('fs')
, path = require('path')
, spawn = require('child_process').spawn
, sys = require('sys')
if (process.argv.length == 4) {
// run a particular test
var test = {
finish: function () {
test._finished++
},
_finished: 0
}
process.on('exit', function () {
if (test._finished == 1) {
process.reallyExit(0)
} else {
if (test._finished == 0) {
sys.puts("Test didn't call finish().")
} else {
sys.puts("Test called finish()", test._finished, "times.")
}
process.reallyExit(1)
}
})
require(process.argv[2])[process.argv[3]](test)
} else {
var test_files = fs.readdirSync('./test')
.filter(function (file) {
return file.match(/\.test\.js$/)
})
.map(function (file) {
return './test/' + file
})
var tests = []
test_files.forEach(function (tf) {
Object.keys(require(tf)).forEach(function (test) {
tests.push({ file: tf, test: test })
})
})
function name(t) {
return path.basename(t.file).match(/^(.+?)\.test\.js$/)[1] + '/' + t.test
}
function run() {
if (tests.length <= 0) return
var t = tests.shift()
sys.print(name(t), '... ')
var output = ''
var runner = spawn(process.execPath, [__filename, t.file, t.test])
runner.on('exit', function (code) {
if (code == 0) {
sys.puts('passed')
} else {
sys.puts('failed')
sys.puts(output)
}
run()
})
runner.stdout.on('data', function (data) {
output += data
})
runner.stderr.on('data', function (data) {
output += data
})
}
run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment