Created
February 17, 2020 06:13
-
-
Save reccanti/6863c845b7e805701c6e3cb96132d6d3 to your computer and use it in GitHub Desktop.
A small, copy-pastable, unit testing framework for Node JS.
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
// Lightweight testing framework | |
// Runs through a nested JS Object until it | |
// finds an entry with a function value. | |
// Otherwise, recursively cycle through the list, adding | |
// descriptive tags along the way | |
function run(tests) { | |
const greenText = text => `\x1b[32m${text}`; | |
const redText = text => `\x1b[31m${text}`; | |
const resetText = text => `\x1b[0m${text}`; | |
// The Test Assertion | |
function expect(condition, result) { | |
if (condition === result) { | |
console.log(greenText(`TRUE`)); | |
return; | |
} | |
console.log(redText(`FALSE | |
\tExpected: ${condition} | |
\tReceived: ${result}`)); | |
} | |
function runBlock(block, tests) { | |
Object.entries(tests).forEach(([desc, value]) => { | |
if (typeof value === 'function') { | |
if (block === "") { | |
console.log(resetText(desc)); | |
} else { | |
console.log(resetText(`${block} - ${desc}`)); | |
} | |
value({ expect }); | |
} else { | |
runBlock(desc, value) | |
} | |
}); | |
} | |
runBlock("", tests); | |
} | |
module.exports = run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment