Skip to content

Commit

Permalink
don't let jasmine swallow errors by ignoring its expectationResultHan…
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann authored Jan 4, 2019
1 parent b7e929f commit 1de136e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
21 changes: 15 additions & 6 deletions packages/wdio-jasmine-framework/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ class JasmineAdapter {
/**
* enable expectHandler
*/
const { expectationResultHandler } = this.jasmineNodeOpts
const handler = typeof expectationResultHandler === 'function'
? expectationResultHandler
: jasmine.Spec.prototype.addExpectationResult
jasmine.Spec.prototype.addExpectationResult = handler
jasmine.Spec.prototype.addExpectationResult = this.getExpectationResultHandler(jasmine)

/**
* wrap commands with wdio-sync
Expand Down Expand Up @@ -193,6 +189,17 @@ class JasmineAdapter {
return message
}

getExpectationResultHandler (jasmine) {
let { expectationResultHandler } = this.jasmineNodeOpts
const origHandler = jasmine.Spec.prototype.addExpectationResult

if (typeof expectationResultHandler !== 'function') {
return origHandler
}

return this.expectationResultHandler(origHandler)
}

expectationResultHandler (origHandler) {
const { expectationResultHandler } = this.jasmineNodeOpts
return function (passed, data) {
Expand All @@ -201,12 +208,14 @@ class JasmineAdapter {
} catch (e) {
/**
* propagate expectationResultHandler error if actual assertion passed
* but the custom handler decides to throw
*/
if (passed) {
passed = false
data = {
passed: false,
message: 'expectationResultHandlerError: ' + e.message
message: 'expectationResultHandlerError: ' + e.message,
error: e
}
}
}
Expand Down
48 changes: 44 additions & 4 deletions packages/wdio-jasmine-framework/tests/adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test('set custom ', async () => {
)
await adapter.run()
adapter.jrunner.jasmine.Spec.prototype.addExpectationResult('foobar')
expect(config.jasmineNodeOpts.expectationResultHandler).toBeCalledWith('foobar')
expect(config.jasmineNodeOpts.expectationResultHandler).toBeCalledWith('foobar', undefined)
})

test('get data from beforeAll hook', async () => {
Expand Down Expand Up @@ -229,6 +229,39 @@ test('formatMessage', () => {
expect(message.duration).toBe(123)
})

test('getExpectationResultHandler returns origHandler if none is given', () => {
const jasmine = { Spec: { prototype: { addExpectationResult: 'foobar' } } }
const config = { jasmineNodeOpts: {} }
const adapter = new JasmineAdapter(
'0-2',
config,
['/foo/bar.test.js'],
{ browserName: 'chrome' },
wdioReporter
)

adapter.expectationResultHandler = jest.fn().mockImplementation(() => 'barfoo')
const handler = adapter.getExpectationResultHandler(jasmine)
expect(handler).toBe('foobar')
})

test('getExpectationResultHandler returns modified origHandler if expectationResultHandler is given', () => {
const jasmine = { Spec: { prototype: { addExpectationResult: 'foobar' } } }
const config = { jasmineNodeOpts: { expectationResultHandler: jest.fn() } }
const adapter = new JasmineAdapter(
'0-2',
config,
['/foo/bar.test.js'],
{ browserName: 'chrome' },
wdioReporter
)

adapter.expectationResultHandler = jest.fn().mockImplementation(() => 'barfoo')
const handler = adapter.getExpectationResultHandler(jasmine)
expect(handler).toBe('barfoo')
expect(adapter.expectationResultHandler).toBeCalledWith('foobar')
})

test('expectationResultHandler', () => {
const origHandler = jest.fn()
const config = { jasmineNodeOpts: { expectationResultHandler: jest.fn() } }
Expand All @@ -248,8 +281,9 @@ test('expectationResultHandler', () => {

test('expectationResultHandler failing', () => {
const origHandler = jest.fn()
const err = new Error('uuups')
const config = { jasmineNodeOpts: { expectationResultHandler: () => {
throw new Error('uuups')
throw err
} } }
const adapter = new JasmineAdapter(
'0-2',
Expand All @@ -261,8 +295,14 @@ test('expectationResultHandler failing', () => {

const resultHandler = adapter.expectationResultHandler(origHandler)
resultHandler(true, 'foobar')
expect(origHandler)
.toBeCalledWith(false, { passed: false, message: 'expectationResultHandlerError: uuups' })
expect(origHandler).toBeCalledWith(
false,
{
passed: false,
message: 'expectationResultHandlerError: uuups',
error: err
}
)
})

test('expectationResultHandler failing with failing test', () => {
Expand Down

0 comments on commit 1de136e

Please sign in to comment.