Skip to content

Commit

Permalink
exception message in json formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
innocentiv committed Nov 24, 2017
1 parent a412c96 commit dcd6269
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/formatter/helpers/error_helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash'
import { format } from 'assertion-error-formatter'

export function formatError(error, colorFns) {
Expand All @@ -10,3 +11,15 @@ export function formatError(error, colorFns) {
}
})
}

export function stringifyException(exception) {
if (_.isString(exception)) return exception

let { message, stack } = exception
if (_.includes(stack, message)) return stack
if (message && stack) return message + '\n' + stack
if (message) return message
if (stack) return stack

return JSON.stringify(exception)
}
41 changes: 41 additions & 0 deletions src/formatter/helpers/error_helpers_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { stringifyException } from './error_helpers'

describe('ErrorHelpers', function() {
describe('stringifyException', function() {
it('return string on string exception', function() {
let error_message = stringifyException('error')
expect(error_message).to.equal('error')
})

it('return message', function() {
let error_message = stringifyException({ message: 'error' })
expect(error_message).to.equal('error')
})

it('return stack', function() {
let error_message = stringifyException({ stack: 'stack' })
expect(error_message).to.equal('stack')
})

it('return message and stack', function() {
let error_message = stringifyException({
message: 'error',
stack: 'stack'
})
expect(error_message).to.equal('error\nstack')
})

it('do not repeat message', function() {
let error_message = stringifyException({
message: 'error',
stack: 'error\nstack'
})
expect(error_message).to.equal('error\nstack')
})

it('stringify an object', function() {
let error_message = stringifyException({ prop: 'error' })
expect(error_message).to.equal('{"prop":"error"}')
})
})
})
2 changes: 1 addition & 1 deletion src/formatter/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as PickleParser from './pickle_parser'

export { default as EventDataCollector } from './event_data_collector'
export { default as KeywordType, getStepKeywordType } from './keyword_type'
export { formatError } from './error_helpers'
export { formatError, stringifyException } from './error_helpers'
export { formatIssue, isIssue } from './issue_helpers'
export { formatLocation } from './location_helpers'
export { formatSummary } from './summary_helpers'
Expand Down
9 changes: 7 additions & 2 deletions src/formatter/json_formatter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import _ from 'lodash'
import Formatter from './'
import Status from '../status'
import { formatLocation, GherkinDocumentParser, PickleParser } from './helpers'
import {
formatLocation,
GherkinDocumentParser,
PickleParser,
stringifyException
} from './helpers'
import { buildStepArgumentIterator } from '../step_arguments'

const {
Expand Down Expand Up @@ -156,7 +161,7 @@ export default class JsonFormatter extends Formatter {
data.result.duration = testStep.result.duration
}
if (status === Status.FAILED && exception) {
data.result.error_message = exception.stack || exception
data.result.error_message = stringifyException(exception)
}
}
if (_.size(testStep.attachments) > 0) {
Expand Down

0 comments on commit dcd6269

Please sign in to comment.