mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
Merge pull request #14111 from wenlong-chen/fix-module-destroy-order
fix(core): Order of module destroy should be the reverse of module init
This commit is contained in:
@@ -43,10 +43,7 @@ describe('OnModuleDestroy', () => {
|
||||
it('should sort modules by distance (topological sort) - DESC order', async () => {
|
||||
@Injectable()
|
||||
class BB implements OnModuleDestroy {
|
||||
public field: string;
|
||||
async onModuleDestroy() {
|
||||
this.field = 'b-field';
|
||||
}
|
||||
onModuleDestroy = Sinon.spy();
|
||||
}
|
||||
|
||||
@Module({
|
||||
@@ -57,13 +54,10 @@ describe('OnModuleDestroy', () => {
|
||||
|
||||
@Injectable()
|
||||
class AA implements OnModuleDestroy {
|
||||
public field: string;
|
||||
constructor(private bb: BB) {}
|
||||
|
||||
async onModuleDestroy() {
|
||||
this.field = this.bb.field + '_a-field';
|
||||
}
|
||||
onModuleDestroy = Sinon.spy();
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [B],
|
||||
providers: [AA],
|
||||
@@ -78,7 +72,8 @@ describe('OnModuleDestroy', () => {
|
||||
await app.init();
|
||||
await app.close();
|
||||
|
||||
const instance = module.get(AA);
|
||||
expect(instance.field).to.equal('b-field_a-field');
|
||||
const aa = module.get(AA);
|
||||
const bb = module.get(BB);
|
||||
Sinon.assert.callOrder(aa.onModuleDestroy, bb.onModuleDestroy);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -402,7 +402,10 @@ export class NestApplicationContext<
|
||||
* modules and its children.
|
||||
*/
|
||||
protected async callDestroyHook(): Promise<void> {
|
||||
const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
|
||||
const modulesSortedByDistance = [
|
||||
...this.getModulesToTriggerHooksOn(),
|
||||
].reverse();
|
||||
|
||||
for (const module of modulesSortedByDistance) {
|
||||
await callModuleDestroyHook(module);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user