Skip to content

Commit

Permalink
Merge pull request #14110 from wenlong-chen/fix-global-module-distance
Browse files Browse the repository at this point in the history
fix(core): calculate module distance after bindGlobalScope
  • Loading branch information
kamilmysliwiec authored Nov 18, 2024
2 parents e965b4a + 5ccc88f commit 3ad835c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
39 changes: 36 additions & 3 deletions integration/hooks/e2e/on-module-init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,39 @@ describe('OnModuleInit', () => {
});

it('should sort modules by distance (topological sort) - DESC order', async () => {
@Injectable()
class CC implements OnModuleInit {
public field: string;

async onModuleInit() {
this.field = 'c-field';
}
}

@Module({})
class C {
static forRoot() {
return {
module: C,
global: true,
providers: [
{
provide: CC,
useValue: new CC(),
},
],
exports: [CC],
};
}
}

@Injectable()
class BB implements OnModuleInit {
public field: string;
constructor(private cc: CC) {}

async onModuleInit() {
this.field = 'b-field';
this.field = this.cc.field + '_b-field';
}
}

Expand All @@ -68,14 +96,19 @@ describe('OnModuleInit', () => {
})
class A {}

@Module({
imports: [A, C.forRoot()],
})
class AppModule {}

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

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

const instance = module.get(AA);
expect(instance.field).to.equal('b-field_a-field');
expect(instance.field).to.equal('c-field_b-field_a-field');
});
});
4 changes: 2 additions & 2 deletions packages/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export class DependenciesScanner {
overrides: options?.overrides,
});
await this.scanModulesForDependencies();
this.calculateModulesDistance();

this.addScopedEnhancersMetadata();
this.container.bindGlobalScope();

this.calculateModulesDistance();
}

public async scanForModules({
Expand Down

0 comments on commit 3ad835c

Please sign in to comment.