问题:
执行测试用例时出现问题
Nest无法解析AWSSecretManagerStringConfigTest(?)的依赖项。请确保_RootTestModule上下文中的索引[0]处的参数testString可用。
我正在使用所有最新版本的nestjs软件包
错误日志:
AWSModule(SecretsManager服务)›应该具有纯字符串秘密
all-test |
all-test | Nest can't resolve dependencies of the AWSSecretManagerStringConfigTest (?). Please make sure that the argument testString at index [0] is available in the _RootTestModule context.
all-test |
all-test | Potential solutions:
all-test | - If testString is a provider, is it part of the current _RootTestModule?
all-test | - If testString is exported from a separate @Module, is that module imported within _RootTestModule?
all-test | @Module({
all-test | imports: [ /* the Module containing testString */ ]
all-test | })
all-test |
all-test | at Injector.lookupComponentInParentModules (node_modules/@nestjs/core/injector/injector.js:190:19)
all-test | at Injector.resolveComponentInstance (node_modules/@nestjs/core/injector/injector.js:146:33)
all-test | at resolveParam (node_modules/@nestjs/core/injector/injector.js:100:38)
all-test | at async Promise.all (index 0)
all-test | at Injector.resolveConstructorParams (node_modules/@nestjs/core/injector/injector.js:115:27)
all-test | at Injector.loadInstance (node_modules/@nestjs/core/injector/injector.js:79:9)
all-test | at Injector.loadProvider (node_modules/@nestjs/core/injector/injector.js:36:9)
all-test | at async Promise.all (index 3)
all-test | at InstanceLoader.createInstancesOfProviders (node_modules/@nestjs/core/injector/instance-loader.js:41:9)
all-test | at node_modules/@nestjs/core/injector/instance-loader.js:27:13
代码:
GITHUB链接:https://github.com/bhushan629/aws
测试案例:
此测试案例对我来说是正确的
test('Shold have plain string secret', async () => {
const response: AWS.SecretsManager.GetSecretValueResponse = {
SecretString: 'hello',
};
AWSMock.setSDKInstance(AWS);
AWSMock.mock('SecretsManager', 'getSecretValue', Promise.resolve(response));
const module: TestingModule = await Test.createTestingModule({
imports: [
AWSModule.forServices([{ serviceName: 'secrets-manager' }]),
AWSModule.forServicesFeature([
{ serviceName: 'secrets-manager', confiiguration: [{ secretName: 'testString', secretType: 'plain' }] },
]),
],
}).compile();
const plainString = module.get('testString');
expect(plainString).toEqual('hello');
AWSMock.restore();
});
但是当我尝试在测试用例下执行时,它给了我错误
test('Should have plain string secret', async () => {
const response: AWS.SecretsManager.GetSecretValueResponse = {
SecretString: 'hello',
};
AWSMock.setSDKInstance(AWS);
AWSMock.mock('SecretsManager', 'getSecretValue', Promise.resolve(response));
const module: TestingModule = await Test.createTestingModule({
imports: [
AWSModule.forServices([{ serviceName: 'secrets-manager' }]),
AWSModule.forServicesFeature([
{ serviceName: 'secrets-manager', confiiguration: [{ secretName: 'testString', secretType: 'plain' }] },
]),
],
providers: [AWSSecretManagerStringConfigTest],
}).compile();
const awsSecretManagerStringConfigTest = module.get(AWSSecretManagerStringConfigTest);
expect(awsSecretManagerStringConfigTest).toBeInstanceOf(AWSSecretManagerStringConfigTest);
expect(awsSecretManagerStringConfigTest.getSecret()).toEqual('hello');
AWSMock.restore();
});
AWSSecretManagerStringConfigTest.ts
import { Injectable, Inject } from '@nestjs/common';
@Injectable()
export class AWSSecretManagerStringConfigTest {
constructor(@Inject('testString') private readonly secret: string) {}
getSecret(): string {
return this.secret;
}
}