Last active
December 11, 2015 11:08
-
-
Save adrianseeley/4591336 to your computer and use it in GitHub Desktop.
Easiest Ever NodeJS Testing
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
/* Output: | |
node test.js | |
Testing: always_pass...passed | |
Testing: always_fail...failed | |
Testing: include_meaningful_results...failed | |
3 Tests Completed! Passed: 1 Failed: 2 | |
Failed Test Output: | |
{"test":"always_fail","passed":false} | |
{"test":"include_meaningful_results","passed":false,"meaningful_data":"This results object will display as a JSON string if this test fails"} | |
*/ | |
var tests = []; | |
var results = []; | |
tests.push(always_pass); | |
tests.push(always_fail); | |
tests.push(include_meaningful_results); | |
start(); | |
function w(m) { process.stdout.write(m); } | |
function wl(m) { process.stdout.write(m + '\n'); } | |
function start() { var next = tests.shift(); next(); } | |
function finish() { wl(results[results.length - 1].passed == true ? '...passed' : '...failed'); if(tests.length) start(); else { var failed_results = []; for(var t = 0; t < results.length; t++) if(!results[t].passed) failed_results.push(results[t]); wl(results.length + ' Tests Completed! Passed: ' + (results.length - failed_results.length) + ' Failed: ' + failed_results.length); if(failed_results.length > 0) { wl('Failed Test Output: '); for(var f = 0; f < failed_results.length; f++) wl(JSON.stringify(failed_results[f])); } process.exit(failed_results.length); } } | |
function always_pass() | |
{ | |
var tag = arguments.callee.name; w('Testing: ' + tag); | |
// Tests that pass are silently moved over | |
results.push({test: tag, passed: true}); | |
finish(); | |
} | |
function always_fail() | |
{ | |
var tag = arguments.callee.name; w('Testing: ' + tag); | |
// Tests that fail will show their results at the end to help expose issues | |
results.push({test: tag, passed: false}); | |
finish(); | |
} | |
function include_meaningful_results() | |
{ | |
var tag = arguments.callee.name; w('Testing: ' + tag); | |
// When tests fail it can be helpful to include meaningful data in results | |
results.push({test: tag, passed: false, meaningful_data: 'This results object will display as a JSON string if this test fails'}); | |
finish(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment