You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Seems that if a provider has an undefined initial value, it cannot be overriden using overrideProvider().
Input Code
constmongoUriProvider={provide: "MONGO_URI",useValue: process.env.MONGO_URI,// this is undefined in TEST env};
@Module({providers: [mongoUriProvider,ConfigService],exports: [ConfigService],})exportclassConfigModule{}
Everything fails because MONGO_URI is not defined:
Nest can't resolve dependencies of the ConfigService (?). Please make sure that the argument MONGO_URI at index [0] is available in the ConfigModule context.
Potential solutions:
- If MONGO_URI is a provider, is it part of the current ConfigModule?
- If MONGO_URI is exported from a separate @Module, is that module imported within ConfigModule?
@Module({
imports: [ /* the Module containing MONGO_URI */ ]
})
This took a couple of hours of troubleshooting to figure out. A workaround is to provide a default non-undefined value. null seems to work:
This was already discussed in the past. You can't bind undefined values to the container (undefined has simply too many meanings, we can't determine whether you did it on purpose or not) so it's not really an issue with the overrideProvider() method.
exportinterfaceValueProvider<T=string|number|boolean|symbol|bigint|object|null>{/** * Injection token */provide: string|symbol|Type<any>|Abstract<any>|Function;/** * Instance of a provider to be injected. */useValue: T;}
It will at least show a compilation error in typescript strict mode:
Not sure if anything can be done about non-strict mode.
Bug Report
Current behavior
Seems that if a provider has an
undefined
initial value, it cannot be overriden usingoverrideProvider()
.Input Code
Then in tests:
Everything fails because
MONGO_URI
is not defined:This took a couple of hours of troubleshooting to figure out. A workaround is to provide a default non-undefined value.
null
seems to work:Expected behavior
Should be able to override the provider during tests even if it is set to undefined.
At a minimum, a warning should be emitted that an undefined provider has been set, if it cannot be fixed for other reasons.
Environment
The text was updated successfully, but these errors were encountered: