Skip to content

Commit

Permalink
feat: add caller filename
Browse files Browse the repository at this point in the history
pimp colors
  • Loading branch information
balazs4 committed Apr 13, 2021
1 parent 05d5957 commit c98596c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
5 changes: 1 addition & 4 deletions bft.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ global.test = require('./').test;
process.argv
.slice(2)
.map((x) => require('path').resolve(x))
.forEach((testfile) => {
process.stdout.write(`${testfile}\n`);
return require(testfile);
});
.forEach((testfile) => require(testfile));
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
/**
* test function for `bft`
*
* @param {string} name - name of test
* @param {string} description - description of test
* @param {function|Promise<void>} [assertion] - test code
*/
module.exports.test = async (name, assertion = null) => {
module.exports.test = async (description, assertion = null) => {
const log = (txt) => {
return process.stdout.write(
process.stdout.hasColors && process.stdout.hasColors() === true
? txt
.replace(/^PASSED/, '\x1b[32mPASSED\x1b[39m')
.replace(/^FAILED/, '\x1b[31mFAILED\x1b[39m')
.replace(/^SKIPPED/, '\x1b[2mSKIPPED\x1b[0m')
.replace(/^PASSED/, '\x1b[32mPASSED\x1b[0m')
.replace(/^FAILED/, '\x1b[31mFAILED\x1b[0m')
.replace(/^SKIPPED/, '\x1b[33mSKIPPED\x1b[0m')
: txt
);
};
const [, , caller] = new Error().stack.split('at ');
const [, filename] = caller.match(/\((.*)\)/);
const origin =
process.stdout.hasColors && process.stdout.hasColors() === true
? `\x1b[2m${filename}\x1b[0m`
: filename;

const name =
process.stdout.hasColors && process.stdout.hasColors() === true
? `\x1b[1m${description}\x1b[0m`
: description;
setImmediate(async () => {
try {
if (assertion === null) {
log(`SKIPPED\t${name}`);
log(`SKIPPED\t${name} << ${origin}`);
return;
}

const result = assertion();
await result;

log(`PASSED\t${name}`);
log(`PASSED\t${name} << ${origin}`);
} catch (err) {
log(`FAILED\t${name}`);
log(`FAILED\t${name} << ${origin}`);
const lines = err.stack
.split('\n')
.map((x) => `\t${x}`)
Expand Down

0 comments on commit c98596c

Please sign in to comment.