From 7bde58b5edfedecb2173e4db7cac037108f221dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Mon, 5 Dec 2022 16:50:37 -0500 Subject: [PATCH] Extract the error reporter --- reporter/consolereporter.js | 25 ------------------------ reporter/errorreporter.js | 39 +++++++++++++++++++++++++++++++++++++ reporter/index.js | 3 ++- 3 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 reporter/errorreporter.js diff --git a/reporter/consolereporter.js b/reporter/consolereporter.js index 6f6f7be..f77b55d 100644 --- a/reporter/consolereporter.js +++ b/reporter/consolereporter.js @@ -15,36 +15,11 @@ const consolereporter = () => { 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 } -import { stackParser } from '../stackparser.js' -const prettyError = (error) => { - if (typeof error == 'string') { - log(chalk.yellowBright(error)) - log('\n') - return - } - - const stack = stackParser(error.stack) - log(chalk.yellowBright(stack.title) + '\n') - - if (stack.diff != '\n ') - log(chalk.white(stack.diff)) - - if (stack.where != '') - log(chalk.gray(stack.where) + '\n') -} - export { consolereporter } \ No newline at end of file diff --git a/reporter/errorreporter.js b/reporter/errorreporter.js new file mode 100644 index 0000000..0792ae3 --- /dev/null +++ b/reporter/errorreporter.js @@ -0,0 +1,39 @@ +import chalk from 'chalk' + +const log = (...args) => process.stdout.write(...args) + +const errorreporter = () => { + const report = { + end: (stats) => { + const { testFail } = stats + + for (const test of testFail) { + log('\n' + chalk.redBright(test.name) + '\n') + prettyError(test.error) + log('\n') + } + } + } + + return report +} + +import { stackParser } from '../stackparser.js' +const prettyError = (error) => { + if (typeof error == 'string') { + log(chalk.yellowBright(error)) + log('\n') + return + } + + const stack = stackParser(error.stack) + log(chalk.yellowBright(stack.title) + '\n') + + if (stack.diff != '\n ') + log(chalk.white(stack.diff)) + + if (stack.where != '') + log(chalk.gray(stack.where) + '\n') +} + +export { errorreporter } \ No newline at end of file diff --git a/reporter/index.js b/reporter/index.js index 5312c73..3432814 100644 --- a/reporter/index.js +++ b/reporter/index.js @@ -1,3 +1,4 @@ export { consolereporter } from './consolereporter.js' export { linereporter } from './linereporter.js' -export { totalreporter } from './totalreporter.js' \ No newline at end of file +export { totalreporter } from './totalreporter.js' +export { errorreporter } from './errorreporter.js' \ No newline at end of file