Skip to content

Commit

Permalink
test: improve test coverage of printErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronhunter authored and okonet committed Jul 6, 2019
1 parent dd1100b commit 0dee25b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/printErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Work-around for duplicated error logs, see #142
const errMsg = err => (err.privateMsg != null ? err.privateMsg : err.message)

module.exports = function printErrors(errorInstance, logger = console) {
module.exports = function printErrors(errorInstance, logger) {
if (Array.isArray(errorInstance.errors)) {
errorInstance.errors.forEach(lintError => {
logger.error(errMsg(lintError))
Expand Down
20 changes: 6 additions & 14 deletions test/printErrors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@ import makeConsoleMock from 'consolemock'
import printErrors from '../src/printErrors'

describe('printErrors', () => {
const originalConsole = console

beforeAll(() => {
console = makeConsoleMock()
})
const logger = makeConsoleMock()

beforeEach(() => {
console.clearHistory()
})

afterAll(() => {
global.console = originalConsole
logger.clearHistory()
})

it('should print plain errors', () => {
const err = new Error('We have a problem')
printErrors(err)
expect(console.printHistory()).toMatchSnapshot()
printErrors(err, logger)
expect(logger.printHistory()).toMatchSnapshot()
})

it('should print Listr nested errors', async () => {
Expand Down Expand Up @@ -59,8 +51,8 @@ describe('printErrors', () => {
try {
await list.run()
} catch (err) {
printErrors(err)
expect(console.printHistory()).toMatchSnapshot()
printErrors(err, logger)
expect(logger.printHistory()).toMatchSnapshot()
}
})
})

0 comments on commit 0dee25b

Please sign in to comment.