diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e0fbfbd..73300753 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Last Changes +- [#443](https://github.com/LaxarJS/laxar/issues/443): testing: initialize eventBusMock with noisy error log - [#448](https://github.com/LaxarJS/laxar/issues/448): documentation: fixed markdown indent in API index diff --git a/docs/api/testing.event_bus_mock.md b/docs/api/testing.event_bus_mock.md index 5733c31c..454599aa 100644 --- a/docs/api/testing.event_bus_mock.md +++ b/docs/api/testing.event_bus_mock.md @@ -33,7 +33,7 @@ published events. | -------- | ---- | ----------- | | _options_ | `Object` | additional options | | _options.nextTick_ | `Object` | an alternative callback for scheduling the next event bus cycle (such as window.setTimeout) | -| _options.errorHandler_ | `Object` | an alternative error handler, e.g. to inspect error conditions during test | +| _options.errorHandler_ | `Object` | an alternative error handler, e.g. to inspect error conditions during test. By default, exceptions thrown by subscribers to the mock will be reported using `window.console.error` | ##### Returns diff --git a/lib/testing/event_bus_mock.js b/lib/testing/event_bus_mock.js index 21103112..7b564bab 100644 --- a/lib/testing/event_bus_mock.js +++ b/lib/testing/event_bus_mock.js @@ -29,7 +29,8 @@ import { create as createLogMock } from './log_mock'; * @param {Object} [options.nextTick] * an alternative callback for scheduling the next event bus cycle (such as window.setTimeout) * @param {Object} [options.errorHandler] - * an alternative error handler, e.g. to inspect error conditions during test + * an alternative error handler, e.g. to inspect error conditions during test. By default, exceptions + * thrown by subscribers to the mock will be reported using `window.console.error` * * @return {EventBusMock} * a fresh mock instance @@ -57,7 +58,11 @@ export function create( { nextTick, errorHandler } = {} ) { createLogMock(), nextTick || fallbackTick, setTimeout, - errorHandler + errorHandler || (( ...args ) => { + if( window.console && window.console.error ) { + window.console.error( ...args ); + } + }) ); /** diff --git a/lib/testing/spec/event_bus_mock_spec.js b/lib/testing/spec/event_bus_mock_spec.js index eaacd264..5756c0b1 100644 --- a/lib/testing/spec/event_bus_mock_spec.js +++ b/lib/testing/spec/event_bus_mock_spec.js @@ -65,6 +65,23 @@ describe( 'An eventBus mock', () => { /////////////////////////////////////////////////////////////////////////////////////////////////////////// + it( 'reports subscriber errors to console.error by default', () => { + spyOn( window.console, 'error' ); + eventBusMock = createEventBusMock(); + let e; + eventBusMock.subscribe( 'myEvent', () => { + e = new Error( 'error' ); + throw e; + } ); + eventBusMock.publish( 'myEvent' ); + eventBusMock.flush(); + + expect( window.console.error ).toHaveBeenCalledWith( jasmine.any( String ), jasmine.any( Object ) ); + expect( window.console.error.calls.mostRecent().args[ 1 ][ 'Exception' ] ).toEqual( e ); + } ); + + /////////////////////////////////////////////////////////////////////////////////////////////////////////// + it( 'allows to synchronously flush pending events', () => { const spyA = jasmine.createSpy( 'A' ); const spyB = jasmine.createSpy( 'B' );