Skip to content

Instantly share code, notes, and snippets.

@reccanti
Created February 17, 2020 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reccanti/6863c845b7e805701c6e3cb96132d6d3 to your computer and use it in GitHub Desktop.
Save reccanti/6863c845b7e805701c6e3cb96132d6d3 to your computer and use it in GitHub Desktop.
A small, copy-pastable, unit testing framework for Node JS.
// 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