diff --git a/lib/event-subscribers.loader.ts b/lib/event-subscribers.loader.ts index 4d3d0f2f..3068a963 100644 --- a/lib/event-subscribers.loader.ts +++ b/lib/event-subscribers.loader.ts @@ -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) => { diff --git a/tests/e2e/module-e2e.spec.ts b/tests/e2e/module-e2e.spec.ts index 13fb5939..b32bf225 100644 --- a/tests/e2e/module-e2e.spec.ts +++ b/tests/e2e/module-e2e.spec.ts @@ -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; @@ -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' }); diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index d58090e4..bd7b2bed 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -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({ @@ -9,6 +10,7 @@ import { EventsProducer } from './events.producer'; wildcard: true, }), ], - providers: [EventsConsumer, EventsProducer], + controllers: [EventsControllerConsumer], + providers: [EventsProviderConsumer, EventsProducer], }) export class AppModule {} diff --git a/tests/src/events-controller.consumer.ts b/tests/src/events-controller.consumer.ts new file mode 100644 index 00000000..aea28cc5 --- /dev/null +++ b/tests/src/events-controller.consumer.ts @@ -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) { + this.eventPayload = payload; + } +} diff --git a/tests/src/events.consumer.ts b/tests/src/events-provider.consumer.ts similarity index 85% rename from tests/src/events.consumer.ts rename to tests/src/events-provider.consumer.ts index bfd4f9c3..1e7de3f9 100644 --- a/tests/src/events.consumer.ts +++ b/tests/src/events-provider.consumer.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { OnEvent } from '../../lib'; @Injectable() -export class EventsConsumer { +export class EventsProviderConsumer { public eventPayload = {}; @OnEvent('test.*')