Last active
April 26, 2017 19:16
-
-
Save jkingsman/e97e76abfd2f99a2fd371dfc75552d01 to your computer and use it in GitHub Desktop.
Microharness mod to support async
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Example microHarness Page</title> | |
</head> | |
<body> | |
<p>Check your console</p> | |
</body> | |
<!-- Include the base testing harness --> | |
<script src="microharness.js"></script> | |
<!-- Add tests and run them --> | |
<script language="JavaScript"> | |
var tests = [ | |
function() { | |
var actual = true; | |
var expected = true; | |
// push an array into testResults with the zeroth element as the label, the first as the expected output and the second as the returned/testing output | |
testResults.push(['world is not on fire', expected, actual]); | |
}, | |
function() { | |
testResults.push(['failing test', 1 === 1, false]); | |
}, | |
function() { | |
setTimeout(function() { | |
testResults.push(['async test', 1 === 1, true]); | |
}, 1000); | |
}, | |
]; | |
// run the tests by calling the run function with your test object | |
runTests(tests); | |
</script> | |
</html> |
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
var testResults = []; | |
var testCount = 0; | |
var completedTestCount = { | |
passed: 0, | |
failed: 0 | |
}; | |
function runTests(tests) { | |
"use strict"; | |
console.log('Beginning ' + Object.keys(tests).length + ' tests'); | |
for (var name in tests) { | |
// run actual tests | |
tests[name](); | |
testCount++; | |
} | |
var checkingInterval = setInterval(function() { | |
// check for results | |
var colors = { | |
p: 'background: #222; color: #bada55', | |
f: 'background: red; color: white' | |
}; | |
var divider = new Array(40).join('-'); | |
while (testResults.length > 0) { | |
// we have new results | |
var thisResult = testResults.pop(); | |
if (thisResult[1] === thisResult[2]) { | |
// test success | |
console.log(thisResult[0] + ' %cPASS', colors.p); | |
completedTestCount.passed++; | |
} else { | |
// test failure | |
console.log(thisResult[0] + ' %cFAIL', colors.f); | |
console.log('↳ got ' + thisResult[1] + ' and expected ' + thisResult[2]); | |
completedTestCount.failed++; | |
} | |
} | |
if ((completedTestCount.passed + completedTestCount.failed) === testCount) { | |
// all tests done | |
console.log(divider); | |
console.log(divider); | |
if (completedTestCount.failed === 0) { | |
console.log('%c' + completedTestCount.passed + ' tests passed (100% success)', colors.p); | |
} else { | |
console.log('%c' + completedTestCount.passed + ' tests passed', colors.p); | |
console.log('%c' + completedTestCount.failed + ' tests failed', colors.f); | |
} | |
clearInterval(checkingInterval); | |
} | |
}, 200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
![image](https://cloud.githubusercontent.com/assets/2365891/25452357/3dfdd160-2a7a-11e7-84ff-9ee739816a8e.png)