Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Calculate module distance after bindGlobalScope #14110

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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