Nestjs 应用程序中“@ntegral/nestjs-sentry”包的依赖注入问题

Emm*_*odu 0 sentry nest

我对 Nestjs 中的这个包有疑问@ntegral/nestjs-sentry。我有一个在我的应用程序中使用的自定义记录器

\n
@Injectable()\nexport class CustomLogger implements LoggerService {\n  constructor(@InjectSentry() private readonly client: SentryService) {}\n\n  log(message: any, ...optionalParams: any[]) {\n    this.client.instance().captureMessage(message, ...optionalParams);\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后我将其注入到用户控制器和 user.controller.spec.ts 中

\n
describe(\'UsersController\', () => {\n  let controller: UsersController;\n\n  beforeEach(async () => {\n    const module: TestingModule = await Test.createTestingModule({\n      controllers: [UsersController],\n      providers: [\n        CustomLogger,\n        UsersService,\n        SentryService,\n      ],\n    }).compile();\n\n    controller = module.get<UsersController>(UsersController);\n  });\n\n  it(\'should be defined\', () => {\n    expect(controller).toBeDefined();\n  });\n});\n\n
Run Code Online (Sandbox Code Playgroud)\n

我收到这个错误

\n
 FAIL  src/users/users.controller.spec.ts (9.449 s)\n  \xe2\x97\x8f UsersController \xe2\x80\xba should be defined\n\n    Nest can\'t resolve dependencies of the CustomLogger (?). Please make sure that the argument Symbol(SentryToken) at index [0] is available in the RootTestModule context.\n\n    Potential solutions:\n    - If Symbol(SentryToken) is a provider, is it part of the current RootTestModule?\n    - If Symbol(SentryToken) is exported from a separate @Module, is that module imported within RootTestModule?\n      @Module({\n        imports: [ /* the Module containing Symbol(SentryToken) */ ]\n      })\n
Run Code Online (Sandbox Code Playgroud)\n

我已尝试将其添加SentryService到规范提供程序中,但这并不能修复错误。有没有人遇到过这个问题,你是如何解决的。

\n

小智 5

我遇到了完全相同的问题。该库似乎对其自己的 Inject 注释使用了不同的标记。我能够通过使用为 SentryService 模拟提供的令牌在测试中修复它。

import { SENTRY_TOKEN } from '@ntegral/nestjs-sentry';

// ...

const module: TestingModule = await Test.createTestingModule({
  providers: [
  // ...
    {
      provide: SENTRY_TOKEN,
      useValue: { debug: jest.fn() }, // provide SentryService Mock here
    },
  ],
})
Run Code Online (Sandbox Code Playgroud)