From 1de136e193a5f2927641450e988ca93c1ed7578d Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Fri, 4 Jan 2019 10:31:23 +0100 Subject: [PATCH] don't let jasmine swallow errors by ignoring its expectationResultHandler (#3240) --- packages/wdio-jasmine-framework/src/index.js | 21 +++++--- .../tests/adapter.test.js | 48 +++++++++++++++++-- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/packages/wdio-jasmine-framework/src/index.js b/packages/wdio-jasmine-framework/src/index.js index 13641ceae76..1868b18eaf7 100644 --- a/packages/wdio-jasmine-framework/src/index.js +++ b/packages/wdio-jasmine-framework/src/index.js @@ -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 @@ -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) { @@ -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 } } } diff --git a/packages/wdio-jasmine-framework/tests/adapter.test.js b/packages/wdio-jasmine-framework/tests/adapter.test.js index 1525a201683..d74a91ec6b7 100644 --- a/packages/wdio-jasmine-framework/tests/adapter.test.js +++ b/packages/wdio-jasmine-framework/tests/adapter.test.js @@ -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 () => { @@ -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() } } @@ -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', @@ -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', () => {