Skip to content

Commit

Permalink
test(): add test for error handling in controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pooreumu committed Aug 3, 2023
1 parent 4daba90 commit 5fd35cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
15 changes: 13 additions & 2 deletions tests/e2e/module-e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,23 @@ describe('EventEmitterModule - e2e', () => {
expect(customConsumer.isEmitted).toBeTruthy();
});

it('should be able to gracefully recover when an unexpected error occurs', async () => {
it('should be able to gracefully recover when an unexpected error occurs from provider', async () => {
const eventsConsumerRef = app.get(EventsProviderConsumer);
await app.init();

const emitter = app.get(EventEmitter2);
const result = emitter.emit('error-handling.event');
const result = emitter.emit('error-handling.provider');

expect(eventsConsumerRef.errorHandlingCalls).toEqual(1);
expect(result).toBeTruthy();
});

it('should be able to gracefully recover when an unexpected error occurs from controller', async () => {
const eventsConsumerRef = app.get(EventsControllerConsumer);
await app.init();

const emitter = app.get(EventEmitter2);
const result = emitter.emit('error-handling.controller');

expect(eventsConsumerRef.errorHandlingCalls).toEqual(1);
expect(result).toBeTruthy();
Expand Down
7 changes: 7 additions & 0 deletions tests/src/events-controller.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { OnEvent } from '../../lib';
@Controller()
export class EventsControllerConsumer {
public eventPayload = {};
public errorHandlingCalls = 0;

@OnEvent('test.*')
onTestEvent(payload: Record<string, any>) {
this.eventPayload = payload;
}

@OnEvent('error-handling.controller')
onErrorHandlingEvent() {
this.errorHandlingCalls++;
throw new Error('This is a test error');
}
}
2 changes: 1 addition & 1 deletion tests/src/events-provider.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class EventsProviderConsumer {
this.stackedEventCalls++;
}

@OnEvent('error-handling.*')
@OnEvent('error-handling.provider')
onErrorHandlingEvent() {
this.errorHandlingCalls++;
throw new Error('This is a test error');
Expand Down

0 comments on commit 5fd35cd

Please sign in to comment.