Skip to content

Commit

Permalink
support non-error objects and browser error objects
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph committed Nov 27, 2017
1 parent 2e02575 commit ca21cfd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 34 deletions.
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ export function format(err, options) {
message = err.message + ''
} else if (typeof err.inspect === 'function') {
message = err.inspect() + ''
} else {
} else if (typeof err === 'string') {
message = err
} else {
message = JSON.stringify(err)
}

let stack = err.stack || message
const startOfMessageIndex = stack.indexOf(message)
if (startOfMessageIndex !== -1) {
if (startOfMessageIndex === -1) {
stack = '\n' + stack
} else {
const endOfMessageIndex = startOfMessageIndex + message.length
message = stack.slice(0, endOfMessageIndex)
stack = stack.slice(endOfMessageIndex) // remove message from stack
Expand Down
106 changes: 74 additions & 32 deletions src/index_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {AssertionError} from 'assert'
import {format} from './'

describe('AssertionErrorFormatter', function() {
Expand All @@ -25,35 +24,45 @@ describe('AssertionErrorFormatter', function() {
describe('with assertion error', function() {
describe('unified diffs', function() {
it('should show string diffs', function() {
const error = new AssertionError({
const error = {
actual: 'foo',
expected: 'bar',
operator: 'to equal'
})
message: '\'foo\' to equal \'bar\'',
stack: '\'foo\' to equal \'bar\'\n line1\n line2\n line3'
}
expect(format(error, this.options)).to.eql(
'<em>AssertionError: \'foo\' to equal \'bar\'</em>\n' +
'<em>\'foo\' to equal \'bar\'</em>\n' +
' <da>+ expected</da> <dr>- actual</dr>\n' +
'\n' +
' <dr>-foo</dr>\n' +
' <da>+bar</da>\n'
' <da>+bar</da>\n' +
'<es>\n' +
' line1\n' +
' line2\n' +
' line3</es>'
)
})

it('should show object diffs', function() {
const error = new AssertionError({
const error = {
actual: {x: 1, y: 2},
expected: {x: 1, y: 3},
operator: 'to equal'
})
message: '{ x: 1, y: 2 } to equal { x: 1, y: 3 }',
stack: '{ x: 1, y: 2 } to equal { x: 1, y: 3 }\n line1\n line2\n line3'
}
expect(format(error, this.options)).to.eql(
'<em>AssertionError: { x: 1, y: 2 } to equal { x: 1, y: 3 }</em>\n' +
'<em>{ x: 1, y: 2 } to equal { x: 1, y: 3 }</em>\n' +
' <da>+ expected</da> <dr>- actual</dr>\n' +
'\n' +
' {\n' +
' "x": 1\n' +
' <dr>- "y": 2</dr>\n' +
' <da>+ "y": 3</da>\n' +
' }\n'
' }\n' +
'<es>\n' +
' line1\n' +
' line2\n' +
' line3</es>'
)
})
})
Expand All @@ -64,50 +73,77 @@ describe('AssertionErrorFormatter', function() {
})

it('should show string diffs', function() {
const error = new AssertionError({
const error = {
actual: 'foo',
expected: 'bar',
operator: 'to equal'
})
message: '\'foo\' to equal \'bar\'',
stack: '\'foo\' to equal \'bar\'\n line1\n line2\n line3'
}
expect(format(error, this.options)).to.eql(
'<em>AssertionError: \'foo\' to equal \'bar\'</em>\n' +
'<em>\'foo\' to equal \'bar\'</em>\n' +
' <dr>actual</dr> <da>expected</da>\n' +
'\n' +
' <dr>foo</dr><da>bar</da>\n'
' <dr>foo</dr><da>bar</da>\n' +
'<es>\n' +
' line1\n' +
' line2\n' +
' line3</es>'
)
})

it('should show object diffs', function() {
const error = new AssertionError({
const error = {
actual: {x: 1, y: 2},
expected: {x: 1, y: 3},
operator: 'to equal'
})
message: '{ x: 1, y: 2 } to equal { x: 1, y: 3 }',
stack: '{ x: 1, y: 2 } to equal { x: 1, y: 3 }\n line1\n line2\n line3'
}
expect(format(error, this.options)).to.eql(
'<em>AssertionError: { x: 1, y: 2 } to equal { x: 1, y: 3 }</em>\n' +
'<em>{ x: 1, y: 2 } to equal { x: 1, y: 3 }</em>\n' +
' <dr>actual</dr> <da>expected</da>\n' +
'\n' +
' {\n' +
' "x": 1\n' +
' "y": <dr>2</dr><da>3</da>\n' +
' }\n'
' }\n' +
'<es>\n' +
' line1\n' +
' line2\n' +
' line3</es>'
)
})
})
})

describe('with other error', function() {
it('colors the error message', function() {
const error = {
message: 'abc',
stack: 'abc\n def\n ghi'
}
//const error2 = new Error('abc')
expect(format(error, this.options)).to.eql(
'<em>abc</em><es>\n' +
' def\n' +
' ghi</es>'
)
describe('message is in the stack', function() {
it('returns the stack only', function() {
const error = {
message: 'abc',
stack: 'abc\n line1\n line2\n line3'
}
expect(format(error, this.options)).to.eql(
'<em>abc</em><es>\n' +
' line1\n' +
' line2\n' +
' line3</es>'
)
})
})

describe('message is not in the stack', function() {
it('returns the message and the stack', function() {
const error = {
message: 'abc',
stack: 'line1\nline2\nline3'
}
expect(format(error, this.options)).to.eql(
'<em>abc</em><es>\n' +
'line1\n' +
'line2\n' +
'line3</es>'
)
})
})
})

Expand All @@ -116,5 +152,11 @@ describe('AssertionErrorFormatter', function() {
expect(format('abc', this.options)).to.eql('<em>abc</em>')
})
})

describe('with object', function() {
it('outputs the json stringified object', function() {
expect(format({x: 1, y: 2}, this.options)).to.eql('<em>{"x":1,"y":2}</em>')
})
})
})
})

0 comments on commit ca21cfd

Please sign in to comment.