Created
April 21, 2021 15:03
-
-
Save Daymannovaes/086bb0e7cdd4d6d921af3d230ba67db7 to your computer and use it in GitHub Desktop.
light tester implementation (describe, it, assert)
This file contains hidden or 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
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