nest icon indicating copy to clipboard operation
nest copied to clipboard

Recursively trigger OnModuleDestroy hook for testing module

Open moesjarraf opened this issue 8 years ago • 3 comments

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I would like to be able to trigger the OnModuleDestroy function for test modules created in my tests. However, this is not possible, the only method exposed is the init() function, which successfully recursively triggers the OnModuleInit function.

  beforeEach(async () => {
    module = await Test.createTestingModule(config).compile();
    await module.init();
  });

Expected behavior

There should be a module.close() function similar to the module.init() function, which then recursively triggers the OnModuleDestroy function for the module and all of its providers.

  afterEach(async () => {
    await module.destroy();
  });

I noticed that a similar function already exists in nest-application.ts. It should work like that.

What is the motivation / use case for changing the behavior?

I use the OnModuleInit and OnModuleDestroy to configure certain modules. I would like to be able to clean up a module after testing, as currently some tests stay stuck after completion. This is because the destroy hook isn't being called.

moesjarraf avatar Jul 24 '18 11:07 moesjarraf

This feature will be available once #903 is merged

kamilmysliwiec avatar Jul 24 '18 20:07 kamilmysliwiec

5.2.0 is here!

kamilmysliwiec avatar Aug 22 '18 13:08 kamilmysliwiec

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 24 '19 16:09 lock[bot]