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

Commit

Permalink
(#443) testing: log eventBusMock subscriber errors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
x1B committed Apr 7, 2017
1 parent 6cec9cf commit f8dcfeb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion docs/api/testing.event_bus_mock.md
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions lib/testing/event_bus_mock.js
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
}
})
);

/**
Expand Down
17 changes: 17 additions & 0 deletions lib/testing/spec/event_bus_mock_spec.js
Expand Up @@ -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' );
Expand Down

0 comments on commit f8dcfeb

Please sign in to comment.