我对 Nestjs 中的这个包有疑问@ntegral/nestjs-sentry。我有一个在我的应用程序中使用的自定义记录器
@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}\nRun Code Online (Sandbox Code Playgroud)\n然后我将其注入到用户控制器和 user.controller.spec.ts 中
\ndescribe(\'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\nRun 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 })\nRun Code Online (Sandbox Code Playgroud)\n我已尝试将其添加SentryService到规范提供程序中,但这并不能修复错误。有没有人遇到过这个问题,你是如何解决的。
小智 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)
| 归档时间: |
|
| 查看次数: |
1049 次 |
| 最近记录: |