Created
March 2, 2016 13:00
-
-
Save jajeffries/df8a8708f6e26413e587 to your computer and use it in GitHub Desktop.
TDD your own test framework
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
// Tests | |
function testATestHasRun() { | |
var wasRun; | |
test(function () { | |
wasRun = true; | |
}); | |
return wasRun; | |
} | |
function testATestCanFail() { | |
var result = test(function() { | |
return false; | |
}); | |
return result === false; | |
} | |
function testRunningMultipleTests(){ | |
var result = testSuite([testATestHasRun, testATestHasRun]); | |
return result[0] === true && result[1] === true; | |
} | |
function testKnowWhichTestHasFailed(){ | |
function failingTest() { | |
return false; | |
} | |
var result = testSuite([failingTest]); | |
return result[0] === "failingTest"; | |
} | |
// Framework | |
function test(func) { | |
var result = func(); | |
return result; | |
} | |
function testSuite(funcs){ | |
var result = []; | |
for (var i = 0; i < funcs.length; i++) { | |
var testResult = test(funcs[i]); | |
if(testResult === true) | |
{ | |
result.push(testResult); | |
} | |
else { | |
result.push(funcs[i].name); | |
} | |
} | |
return result; | |
} | |
// Runner | |
console.log(testSuite([testATestHasRun,testATestCanFail,testRunningMultipleTests, testKnowWhichTestHasFailed])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment