metaltest/consolereporter.js

56 lines
1.1 KiB
JavaScript

import chalk from 'chalk'
const log = (...args) => process.stdout.write(...args)
const consolereporter = () => {
const report = {
start: (title) => {
log(chalk.cyanBright(title) + ' ')
},
success: (test) => {
log(chalk.greenBright('✓ '))
},
fail: (test, e) => {
log(chalk.redBright('✗ '))
},
end: (stats) => {
const { testFail } = stats
log('\n')
for (const test of testFail) {
log('\n' + chalk.redBright(test.name) + '\n')
prettyError(test.error)
log('\n')
}
}
}
return report
}
const prettyError = (error) => {
if (typeof error == 'string') {
log(chalk.yellowBright(error))
log('\n')
return
}
const msg = error.stack
const i = msg.indexOf('\n')
const title = msg.slice(0, i)
log(chalk.yellowBright(title) + '\n')
const at = msg.indexOf(' at ')
const diff = msg.slice(i + 1, at - 1)
if (diff != '\n ')
log(chalk.white(diff))
const atEnd = msg.slice(at).indexOf('\n')
const where = msg.slice(at + 3, at + atEnd)
if (where != '')
log(chalk.gray(where) + '\n')
}
export { consolereporter }