Last active
February 4, 2018 22:47
-
-
Save sidmani/6c9188781c7ba83a115845e804ca8bb0 to your computer and use it in GitHub Desktop.
Minimalist Node.js unit testing script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2018 Sid Mani | |
// https://sidmani.com | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
var allTests = { | |
// add your test files here, like this: | |
testGroup1: require('./testGroup1.js') | |
}; | |
// in testGroup1.js: | |
// module.exports = { | |
// testCase1: { description: 'test case 1', | |
// fn: function() { return true } // your test case }, | |
// testCase2: ... | |
// .... | |
// } | |
// to test: | |
// $ node unit_test.js | |
// easy. no annoying configuration or bloating. | |
// options: | |
// -d <name> | |
// only run <name> test group | |
// -v | |
// verbose mode | |
// -n | |
// don't catch errors | |
// This file is a part of the protochan project. | |
// https://github.com/sidmani/protochan | |
// https://www.sidmani.com/?postid=3 | |
function runTests() { | |
var verbose = (process.argv.indexOf('-v') > -1); | |
var noCatch = (process.argv.indexOf('-n') > -1); | |
let debugIdx = process.argv.indexOf('-d'); | |
var debug = (debugIdx > -1 ? process.argv[debugIdx+1] : undefined); | |
if (debugIdx > -1 && !debug) { | |
process.stdout.write('Error: -d option must specify name.\n'); | |
return; | |
} | |
var numSuccess = 0; | |
var numFailure = 0; | |
console.log('RUNNING TESTS...'); | |
for (var groupName in allTests) { | |
if (debug && groupName !== debug) { | |
continue; | |
} | |
var testGroup = allTests[groupName]; | |
var success = true; | |
process.stdout.write('π€ RUNNING GROUP: ' + groupName + ' (' + testGroup.length + (verbose?')\n':') ')); | |
for (testCase in testGroup) { | |
let testPass = true; | |
let error = undefined; | |
if (testGroup[testCase].dual) { | |
try { | |
testGroup[testCase].fn(true); | |
// ok | |
} catch (e) { | |
if (noCatch) { | |
throw e; | |
} | |
testPass = false; | |
} | |
try { | |
testGroup[testCase].fn(false); | |
testPass = false; | |
} catch (e) { | |
// ok | |
} | |
} else { | |
try { | |
testGroup[testCase].fn(); | |
testPass = true; | |
} catch (e) { | |
if (noCatch && !testGroup[testCase].shouldFail) { | |
throw e; | |
} else { | |
error = e; | |
testPass = false; | |
} | |
} | |
if (testGroup[testCase].shouldFail) { | |
testPass = !testPass; | |
} | |
} | |
printTestOutput(testGroup[testCase], testPass, error, verbose, testGroup[testCase].dual); | |
if (testPass) { | |
numSuccess += 1; | |
} else { | |
numFailure += 1; | |
success = false; | |
} | |
} | |
if (verbose) { | |
process.stdout.write((success?'β ':'π«') + ' GROUP ' + groupName + ' completed.' + '\n'); | |
} else { | |
process.stdout.write('\n'); | |
} | |
} | |
var numTests = (numSuccess + numFailure); | |
process.stdout.write((numFailure==0?'β ':'π« ') + numTests + ' test' + (numTests==1?'':'s') + ' completed with ' + numFailure + ' error' + (numFailure==1? | |
'.\n':'s.\n')); | |
process.exit(numFailure); | |
}; | |
function printTestOutput(testCase, pass, error, verbose, dual) { | |
if (pass) { | |
process.stdout.write('β '); | |
if (verbose) { | |
if (dual) { | |
process.stdout.write(' π ' + testCase.description + '\n'); | |
} else { | |
process.stdout.write(' ' + testCase.description + '\n'); | |
} | |
} | |
} else { | |
process.stdout.write('π«'); | |
if (verbose) { | |
if (dual) { | |
process.stdout.write(' π ' + testCase.description + '\n'); | |
} else { | |
process.stdout.write(' ' + testCase.description + '\n'); | |
} | |
if (error) { | |
process.stdout.write(' β³ ERROR: ' + error +'\n'); | |
} | |
} | |
} | |
} | |
runTests(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment