nestjs / event-emitter

Event Emitter module for Nest framework (node.js) 🦋

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Jest Unit tests only emit events when intializing full AppModule

DanielMenke opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When I assemble aTestingModule which does not resemble the full AppModule, my event listeners don't get registered at the EventEmitter

With this code:

const module: TestingModule = await Test.createTestingModule({
      imports: [EventEmitterModule.forRoot()],
      providers: [RecipesService, EventEmitter2],
    }).compile()
   
const app = await module.createNestApplication();
await app.init();

the events don't work,

If I initialize the complete AppModule like this:

 const module: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

 const app = await module.createNestApplication();
 await app.init();

the events work fine.

this is my AppModule:

 @Module({
  imports: [
    EventEmitterModule.forRoot(),
    RecipesModule,
  ]
})
export class AppModule {}

Minimum reproduction code

Steps to reproduce

yarn test

Expected behavior

Events should be emitted and listened to in both scenarios

Package version

1.4.1

NestJS version

8.4.7

Node.js version

18.10.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

commented

Token EventEmitter2 not initialized provider. EventEmitterModule.forRoot() initialize provider by token EventEmitter2 and return EventEmitter2 instance.

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

@kamilmysliwiec

Its my bad.

The problem is that by putting EventEmitter2 in the providers list, the emitter provided by EventEmitterModule.forRoot() gets overwritten by a new EventEmitter2 instance without any listeners. Sorry for opening this as a bug.

@DanielMenke Thank you so much for following up on this, you really helped me out. I did the exact same thing in my event service and it was driving me nuts for days

@DanielMenke if possible can provide a snippet on how I can use it. I'm trying to a scenario where once the event gets triggered I want to test if the functions registered with @OnEvent gets called. I'm kind of stuck it would be nice if you can provide some snippet on how do did it since my mocks are not emitting the events as well as they don't have any listeners attached to them.

@BruceWayneTheSupermanSlayer I did it as shown above, with the slight difference that I didn't list the EventEmitter2 class inside my TestingModules provider-Array.

const module: TestingModule = await Test.createTestingModule({
      imports: [EventEmitterModule.forRoot()],
      providers: [RecipesService],
    }).compile()

INSTEAD OF


const module: TestingModule = await Test.createTestingModule({
      imports: [EventEmitterModule.forRoot()],
      providers: [RecipesService, EventEmitter2],
    }).compile()
   

@DanielMenke then how do you call the emit or emitAsync from your test case since it's not part of the provider any more and I actually want to test if we emit the event, the lister gets called . Sorry if this is too dumb question for you.

I simply use my actual implementation inside my test case and only mock the parts that I don't really care about in the test scenario. This means I just use the functions of the Service that emit something. This way I don't have to explicitly use the eventEmitter inside my test case, it is only used by the service that I want to test. If you mock the parts of your implementation which call the emit functions you would have to supply them with a new Instance of the eventEmitter. I can't tell you how to do that atm.