nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.76k stars 7.64k forks source link

Unit Test - Nest can't resolve dependencies of the testString (?). Please make sure that the argument SECRET_MANAGER_SERVICE at index [0 ] is available in the SecretManagerServiceModule context. #3731

Closed lakshnachadha closed 4 years ago

lakshnachadha commented 4 years ago

Bug Report

Current behavior

Getting below error

● SecretsManagerServiceModule › #forFeature() › Shold have plain string secret (without id)

    Nest can't resolve dependencies of the testString (?). Please make sure that the argument SECRET_MANAGER_SERVICE at index [0] is available in the SecretManagerServiceModule context.

    Potential 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

Input Code

Please find attached code aws.zip

tests/unit/services/secrets-manager/secrets-manager.module.spec.ts Below Test case not able to resolve dependency

test('Shold 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');
    });

Expected behavior

this test case should be passed

Possible Solution

Environment


Nest version: 6.10.12


For Tooling issues:
- Node version: v12.14.0 
- Platform:  Macbook Air

Others:

Boshen commented 4 years ago

Your're trying to inject SECRET_MANAGER_SERVICE in src/services/secrets-manager/secrets.provider.ts: inject: [getSecretsManagerToken(secretsManagerServiceFeatureOptions.id)], But it's not injected any where.

lakshnachadha commented 4 years ago

Your're trying to inject SECRET_MANAGER_SERVICE in src/services/secrets-manager/secrets.provider.ts: inject: [getSecretsManagerToken(secretsManagerServiceFeatureOptions.id)], But it's not injected any where.

SecretManagerServiceModule.forRoot function exports SECRET_MANAGER_SERVICE provider. i have follewed the same structure as nestjs/mongoose library

Boshen commented 4 years ago

What are you trying to achieve with your test? forRoot or forFeature should be used independently, not together - SECRET_MANAGER_SERVICE should be provided at the module level, they are not shared cross modules, you have to either import its containing module or inject it somehow.

Since its a test case, you should provide it manually in your test with .overrideProvider and .useValue.

Either case this is not a bug with nestjs.

kamilmysliwiec commented 4 years ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.