Skip to content

Instantly share code, notes, and snippets.

@Daymannovaes
Created April 21, 2021 15:03
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 Daymannovaes/086bb0e7cdd4d6d921af3d230ba67db7 to your computer and use it in GitHub Desktop.
Save Daymannovaes/086bb0e7cdd4d6d921af3d230ba67db7 to your computer and use it in GitHub Desktop.
light tester implementation (describe, it, assert)
const path = require('path');
const chalk = require('chalk');
const fileToTest = process.argv[2];
if(!fileToTest) throw new Error('define the file to test');
let PADDING = -4;
let success = 0;
let fails = 0;
function describe(suite, fn = function(){}) {
PADDING += 4;
console.log(chalk.yellow(' '.repeat(PADDING) + suite));
fn();
PADDING -= 4;
}
function it(suite, fn = function(){}) {
try {
fn();
success++;
console.log(chalk.green(' '.repeat(PADDING + 4) + '✓ ' + suite));
} catch(error) {
console.log(chalk.red(' '.repeat(PADDING + 4) + 'x ' + suite + ' - ' + error));
fails++;
}
}
function assert(result, message) {
if(result !== true) throw (message || new Error().stack);
}
global.describe = describe;
global.it = it;
global.assert = assert;
require(path.resolve(fileToTest));
console.log('\n' + chalk.red(`${fails} tests failed`) + ', ' + chalk.green(`${success} tests succeed`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment