Package Exports
- @japa/errors-printer
Readme
@japa/errors-printer
Print errors produced by the Japa tests runner
Installation
Install the package from the npm registry as follows:
npm i @japa/errors-printer
# yarn
yarn add @japa/errors-printerUsage
You can print errors produced by japa test runner as follows.
import { ErrorsPrinter } from '@japa/errors-printer'
const printer = new ErrorsPrinter()
const error = new Error('boom')
await printer.printError(error)Most of the times, you will find yourself printing errors using the Japa test summary. Here is how you can go about doing it.
import { ErrorsPrinter } from '@japa/errors-printer'
const printer = new ErrorsPrinter()
// assuming you have the runner instance
const summary = runner.getSummary()
/**
* Printing all the errors inside the failure tree
*/
for (let suite in summary.failureTree) {
await printer.printErrors(suite.name, suite.errors)
for (let groupOrTest in suite.children) {
if (groupOrTest.type === 'test') {
await printer.printErrors(groupOrTest.title, groupOrTest.errors)
} else {
await printer.printErrors(groupOrTest.title, groupOrTest.errors)
for (let group in groupOrTest.children) {
await printer.printErrors(group.title, group.errors)
}
}
}
}API
Following are the available methods.
printError()
Accepts error as the only argument. If the error is an assertion error, then the diff will be displayed. Otherwise, the error stack is printed.
Assertion diff
import { Assert } from '@japa/assert'
import { ErrorsPrinter } from '@japa/errors-printer'
const printer = new ErrorsPrinter()
try {
new Assert().deepEqual({ id: 1 }, { id: 2 })
} catch (error) {
await printer.printError(error)
}
Jest error
import expect from 'expect'
import { ErrorsPrinter } from '@japa/errors-printer'
const printer = new ErrorsPrinter()
try {
expect({ bar: 'baz' }).toEqual(expect.not.objectContaining({ bar: 'baz' }))
} catch (error) {
await printer.printError(error)
}
Error stack
import { ErrorsPrinter } from '@japa/errors-printer'
const printer = new ErrorsPrinter()
await printer.printError(new Error('boom'))
printErrors
Print an array of errors produced by the Japa test runner summary. The method accepts the following arguments.
label: The error label to print. Usually, it will be test title or the group title.errors: An array of errors in the following format.{ phase: string, error: Error }
await printer.printErrors('test 1', [
{
phase: 'test',
error: new Error('test failed')
},
{
phase: 'teardown',
error: new Error('teardown failed')
}
])