Skip to content

Commit

Permalink
Merge pull request #182 from Brad-Turner/fix/error-logging-count
Browse files Browse the repository at this point in the history
fix(logging): fix reported errors count
  • Loading branch information
marcolink authored Jul 12, 2022
2 parents 338b089 + cb8636a commit eac5511
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ export function formatLogMessageLogfile (logMessage) {
// Display all errors
export function displayErrorLog (errorLog) {
if (errorLog.length) {
const warningsCount = errorLog.filter((error) => Object.prototype.hasOwnProperty.call(error, 'warning')).length
const errorsCount = errorLog.filter((error) => Object.prototype.hasOwnProperty.call(error, 'warning')).length
console.log(`\n\nThe following ${errorsCount} errors and ${warningsCount} warnings occurred:\n`)
const count = errorLog.reduce((count, curr) => {
if (Object.prototype.hasOwnProperty.call(curr, 'warning')) count.warnings++
else if (Object.prototype.hasOwnProperty.call(curr, 'error')) count.errors++
return count
}, { warnings: 0, errors: 0 })

console.log(`\n\nThe following ${count.errors} errors and ${count.warnings} warnings occurred:\n`)

errorLog
.map((logMessage) => `${format(parseISO(logMessage.ts), 'HH:mm:ss')} - ${formatLogMessageOneLine(logMessage)}`)
Expand Down
13 changes: 10 additions & 3 deletions test/logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,19 @@ test('format log file log message with level info', () => {
})

test('displays error log well formatted', () => {
displayErrorLog(exampleErrorLog)
const extendedExampleErrorLog = [...exampleErrorLog, {
ts: new Date('2022-01-01T01:05:43+01:00').toJSON(),
level: 'warning',
warning: 'another warning'
}]

displayErrorLog(extendedExampleErrorLog)

expect(consoleLogSpy.mock.calls).toHaveLength(3)
expect(consoleLogSpy.mock.calls[0][0]).toContain('The following 1 errors and 1 warnings occurred:')
expect(consoleLogSpy.mock.calls).toHaveLength(4)
expect(consoleLogSpy.mock.calls[0][0]).toContain('The following 1 errors and 2 warnings occurred:')
expect(consoleLogSpy.mock.calls[1][0]).toMatch(/\d{2}:\d{2}:\d{2} - warning text/)
expect(consoleLogSpy.mock.calls[2][0]).toMatch(/\d{2}:\d{2}:\d{2} - Error: error message/)
expect(consoleLogSpy.mock.calls[3][0]).toMatch(/\d{2}:\d{2}:\d{2} - another warning/)
})

test('does not displays error log when empty', () => {
Expand Down

0 comments on commit eac5511

Please sign in to comment.