Encountered Problem:
Facing the following issue while running a unit test case
Nest is unable to resolve the dependencies of the testString (?). Please ensure that the argument SECRET_MANAGER_SERVICE at index [0] is available in the context of SecretManagerServiceModule.
Error Logs:
SecretsManagerServiceModule › #forFeature() › Should have plain string secret (without id)
Nest is unable to resolve the dependencies of the testString (?). Please ensure that the argument SECRET_MANAGER_SERVICE at index [0] is available in the context of SecretManagerServiceModule.
Possible solutions:
- If SECRET_MANAGER_SERVICE is a provider, is it part of the current SecretManagerServiceModule?
- If SECRET_MANAGER_SERVICE is exported from a separate @Module, is that module imported within SecretManagerServiceModule?
@Module({
imports: [ /* the Module containing SECRET_MANAGER_SERVICE */ ]
})
at Injector.lookupComponentInParentModules (node_modules/@nestjs/core/injector/injector.js:190:19)
at Injector.resolveComponentInstance (node_modules/@nestjs/core/injector/injector.js:146:33)
at resolveParam (node_modules/@nestjs/core/injector/injector.js:100:38)
at async Promise.all (index 0)
at Injector.resolveConstructorParams (node_modules/@nestjs/core/injector/injector.js:115:27)
at Injector.loadInstance (node_modules/@nestjs/core/injector/injector.js:79:9)
at Injector.loadProvider (node_modules/@nestjs/core/injector/injector.js:36:9)
at async Promise.all (index 3)
at InstanceLoader.createInstancesOfProviders (node_modules/@nestjs/core/injector/instance-loader.js:41:9)
at node_modules/@nestjs/core/injector/instance-loader.js:27:13
Code Snippet:
Github Link: https://github.com/bhushan629/aws
test('Should have plain string secret (without id)', async () => {
const response: AWS.SecretsManager.GetSecretValueResponse = {
SecretString: 'hello',
};
AWSMock.mock('SecretsManager', 'getSecretValue', Promise.resolve(response));
const module: TestingModule = await Test.createTestingModule({
imports: [
SecretManagerServiceModule.forRoot({}),
SecretManagerServiceModule.forFeature({
confiiguration: [{ secretName: 'testString', secretType: 'plain' }],
}),
],
}).compile();
const plainString = module.get('testString');
expect(plainString).toEqual('hello');
});
I have attempted to emulate the structure of nestjs/mongoose (https://github.com/nestjs/mongoose) but I am facing issues. Does anyone know what could be wrong with this code?
How to Implement:
app.module.ts : This will initialize SecretsManager
@Module({
imports: [SecretManagerServiceModule.forRoot({})],
})
class AppModule {}
sample.module.ts : once secrets manager is initialized, we can use the forFeature function to inject secrets into any providers
@Module({
imports: [
SecretManagerServiceModule.forFeature({
confiiguration: [{ secretName: 'testString', secretType: 'plain' }],
}),
],
providers: [SampleService],
})
class SampleModule {}
sample.service.ts
@Injectable()
class SampleService {
constructor(@Inject('testString') testStr: string) {}
}