Skip to content

Commit

Permalink
fix(@nestjs/event-emitter): Consume events from controllers.
Browse files Browse the repository at this point in the history
Previously, the OnEvent decorator would only be honoured within
providers, not controllers.
  • Loading branch information
taj-p committed Feb 20, 2021
1 parent 5a6ac8a commit c8e6788
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/event-subscribers.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class EventSubscribersLoader

loadEventListeners() {
const providers = this.discoveryService.getProviders();
providers
const controllers = this.discoveryService.getControllers();
[...providers, ...controllers]
.filter(wrapper => wrapper.isDependencyTreeStatic())
.filter(wrapper => wrapper.instance)
.forEach((wrapper: InstanceWrapper) => {
Expand Down
14 changes: 11 additions & 3 deletions tests/e2e/module-e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { EventsConsumer } from '../src/events.consumer';
import { EventsControllerConsumer } from '../src/events-controller.consumer';
import { EventsProviderConsumer } from '../src/events-provider.consumer';

describe('EventEmitterModule - e2e', () => {
let app: INestApplication;
Expand All @@ -14,8 +15,15 @@ describe('EventEmitterModule - e2e', () => {
app = module.createNestApplication();
});

it(`should emit a "test-event" event`, async () => {
const eventsConsumerRef = app.get(EventsConsumer);
it(`should emit a "test-event" event to providers`, async () => {
const eventsConsumerRef = app.get(EventsProviderConsumer);
await app.init();

expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' });
});

it(`should emit a "test-event" event to controllers`, async () => {
const eventsConsumerRef = app.get(EventsControllerConsumer);
await app.init();

expect(eventsConsumerRef.eventPayload).toEqual({ test: 'event' });
Expand Down
6 changes: 4 additions & 2 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { EventEmitterModule } from '../../lib';
import { EventsConsumer } from './events.consumer';
import { EventsControllerConsumer } from './events-controller.consumer';
import { EventsProviderConsumer } from './events-provider.consumer';
import { EventsProducer } from './events.producer';

@Module({
Expand All @@ -9,6 +10,7 @@ import { EventsProducer } from './events.producer';
wildcard: true,
}),
],
providers: [EventsConsumer, EventsProducer],
controllers: [EventsControllerConsumer],
providers: [EventsProviderConsumer, EventsProducer],
})
export class AppModule {}
12 changes: 12 additions & 0 deletions tests/src/events-controller.consumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller, Injectable } from '@nestjs/common';
import { OnEvent } from '../../lib';

@Controller()
export class EventsControllerConsumer {
public eventPayload = {};

@OnEvent('test.*')
onTestEvent(payload: Record<string, any>) {
this.eventPayload = payload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { OnEvent } from '../../lib';

@Injectable()
export class EventsConsumer {
export class EventsProviderConsumer {
public eventPayload = {};

@OnEvent('test.*')
Expand Down

0 comments on commit c8e6788

Please sign in to comment.