Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Add additional info to test result for better output
Browse files Browse the repository at this point in the history
  • Loading branch information
roadhump committed Nov 25, 2015
1 parent afbe379 commit 6565282
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ var formatError = function (error) {
return message
}

var processAssertionError = function (error_) {

var error

if (window.Mocha && error_.hasOwnProperty('showDiff')) {

error = {
name: error_.name,
message: error_.message,
showDiff: error_.showDiff
}

if (error.showDiff) {
error.actual = window.Mocha.utils.stringify(error_.actual)
error.expected = window.Mocha.utils.stringify(error_.expected)
}

}

return error

}

// non-compliant version of Array::reduce.call (requires memo argument)
var arrayReduce = function (array, reducer, memo) {
for (var i = 0, len = array.length; i < len; i++) {
Expand Down Expand Up @@ -64,18 +87,25 @@ var createMochaReporterConstructor = function (tc, pathname) {

runner.on('test', function (test) {
test.$errors = []
test.$assertionErrors = []
})

runner.on('pending', function (test) {
test.pending = true
})

runner.on('fail', function (test, error) {

var simpleError = formatError(error)
var assertionError = processAssertionError(error)

if (test.type === 'hook') {
test.$errors = [formatError(error)]
test.$errors = [simpleError]
test.$assertionErrors = assertionError ? [assertionError] : []
runner.emit('test end', test)
} else {
test.$errors.push(formatError(error))
test.$errors.push(simpleError)
if (assertionError) test.$assertionErrors.push(assertionError)
}
})

Expand All @@ -89,7 +119,8 @@ var createMochaReporterConstructor = function (tc, pathname) {
success: test.state === 'passed',
skipped: skipped,
time: skipped ? 0 : test.duration,
log: test.$errors || []
log: test.$errors || [],
assertionErrors: test.$assertionErrors || []
}

var pointer = test.parent
Expand Down
82 changes: 82 additions & 0 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
createConfigObject,
mochaConfig: true,
formatError: true,
processAssertionError: true,
MockSocket: true,
Emitter: true,
sinon: true,
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('adapter mocha', function () {
expect(result.success).to.eq(true)
expect(result.skipped).to.to.eql(false)
expect(result.log instanceof Array).to.eq(true)
expect(result.assertionErrors instanceof Array).to.eq(true)
expect(result.time).to.eq(123)
})

Expand Down Expand Up @@ -150,6 +152,7 @@ describe('adapter mocha', function () {
expect(result.success).to.to.eql(false)
expect(result.skipped).to.to.eql(false)
expect(result.log).to.deep.eq(['Big trouble.', 'Another fail.'])
expect(result.assertionErrors).to.be.empty
})

var mockMochaResult = {
Expand All @@ -167,6 +170,36 @@ describe('adapter mocha', function () {
expect(tc.result.called).to.eq(true)
})

it('should report failed mocha result', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.log).to.deep.eq(['Big trouble.', 'Another fail.'])
expect(result.assertionErrors).to.deep.eq([{
name: 'AssertionError',
message: 'Big trouble.',
showDiff: false
}])
})

var mockMochaResult = {
parent: {title: 'desc2', root: true},
state: 'failed',
title: 'should do something'
}

runner.emit('test', mockMochaResult)
runner.emit('fail', mockMochaResult, {
name: 'AssertionError',
message: 'Big trouble.',
showDiff: false,
actual: 1,
expected: 2
})
runner.emit('fail', mockMochaResult, {message: 'Another fail.'})
runner.emit('test end', mockMochaResult)

expect(tc.result.called).to.eq(true)
})

it('should report suites', function () {
sandbox.stub(tc, 'result', function (result) {
expect(result.suite).to.deep.eq(['desc1', 'desc2'])
Expand Down Expand Up @@ -377,4 +410,53 @@ describe('adapter mocha', function () {
})

})

describe('processAssertionError', function () {
it('should create object from mocha error', function () {

var err = new Error()
err.name = 'AssertionError'
err.message = 'expected \'something\' to deeply equal \'something else\''
err.showDiff = true
err.actual = {baz: 'baz', foo: null, bar: function () {}}
err.expected = {baz: 42, foo: undefined}

var error = processAssertionError(err)

expect(Object.keys(error)).to.be.eql(['name', 'message', 'showDiff', 'actual', 'expected'])
expect(error.name).to.equal('AssertionError')
expect(error.message).to.equal('expected \'something\' to deeply equal \'something else\'')
expect(error.showDiff).to.be.true
expect(error.actual).to.equal('{\n "bar": [Function]\n "baz": "baz"\n "foo": [null]\n}')
expect(error.expected).to.equal('{\n "baz": 42\n "foo": [undefined]\n}')
})

it('should not create object from simple error', function () {

var err = new Error('Something wrong')

var error = processAssertionError(err)

expect(error).to.be.undefined
})

it('should not pass actual and expected if showDiff is off', function () {

var err = new Error()
err.message = 'expected \'something\' to deeply equal \'something else\''
err.showDiff = false
err.actual = {baz: 'baz', foo: null, bar: function () {}}
err.expected = {baz: 42, foo: undefined}

var error = processAssertionError(err)

expect(Object.keys(error)).to.be.eql(['name', 'message', 'showDiff'])
expect(error.name).to.equal('Error')
expect(error.message).to.equal('expected \'something\' to deeply equal \'something else\'')
expect(error.showDiff).to.be.false
expect(error).to.not.have.property('actual')
expect(error).to.not.have.property('expected')
})
})

})

0 comments on commit 6565282

Please sign in to comment.