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
Overide providers from imported module (not in testing context) #13494
Comments
By the way, you don't even export any providers from import { NestFactory } from '@nestjs/core';
import { Controller, Get, Inject, Module } from '@nestjs/common';
const tokenSymbol = Symbol('Some-Token');
@Module({
providers: [{ provide: tokenSymbol, useValue: 'Batman' }],
exports: [tokenSymbol],
})
class FirstModule {}
@Controller()
export class FirstController {
constructor(@Inject(tokenSymbol) private batmanOrRobin: string) {}
@Get()
getHello(): string {
return this.batmanOrRobin;
}
}
@Module({
imports: [FirstModule],
providers: [{ provide: tokenSymbol, useValue: 'Robin' }],
controllers: [FirstController],
})
class SecondModule {}
@Module({
imports: [SecondModule],
})
export class AppModule {}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap(); |
I have updated my test to use |
First of all, I agree with you that it's confusing, but it looks like I can understand why it happens. import { NestFactory } from '@nestjs/core';
import { Inject, Module } from '@nestjs/common';
const tokenSymbol = Symbol('Some-Token');
@Module({
providers: [{ provide: tokenSymbol, useValue: 'Batman' }],
exports: [tokenSymbol],
})
class FirstModule {}
@Module({
imports: [FirstModule],
providers: [{ provide: tokenSymbol, useValue: 'Robin' }],
})
class SecondModule {
constructor(@Inject(tokenSymbol) public batmanOrRobin: string) {}
}
async function bootstrap() {
const app = await NestFactory.create(SecondModule);
const value1 = app.get(tokenSymbol);
console.log('per application:', value1);
const value2 = app.get(SecondModule);
console.log('per module:', value2.batmanOrRobin);
}
bootstrap(); This code prints the following values to the console:
The So the value returned by the Nest DI container depends on the scope. |
Thanks @KostyaTretyak . Yes, I have run a similar check and am able to verify the returned values. |
Finally was able to make the tests pass (used
|
Is there an existing issue for this?
Current behavior
I am trying to override a provider imported into module B from module A by provding an override of the said provider in the
Module
decorator of module B.However when I am trying to get an instance of the provider by a token, I am always getting the provider definition as given in Module A, instead of the Module B.
I have created a sample repo with the failing test @ https://github.com/chandu/nest-override-modules
Te be speicifc this file: https://github.com/chandu/nest-override-modules/blob/main/src/override.spec.ts#L7
Snippet of the code is below:
Minimum reproduction code
https://github.com/chandu/nest-override-modules
Steps to reproduce
pnpm install
pnpm test
Expected behavior
I assumed that the provider definitions are resolved in a hierarchical way, i.e look for the provider in current module, if not found look into the providers that are expored from the imported modules etc.
I would have expected the value resolved from the container for the token to be 'Robin' instead of 'Batman' in the code snippet above.
But it's always resolving the value 'Batman' though this provider is not exported at all from the FirstModule.
What is the expected behavior of provider resolution?
Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
No response
NestJS version
10
Packages versions
Node.js version
20
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: