[英]How to mock a nest-winston logger dependency in nestjs unit tests
This question already exists , but it is marked as solved and the solution doesn't work at all for me. 这个问题已经存在,但它被标记为已解决,并且该解决方案对我根本不起作用。
I want to mock a nest-winston logger that is a dependency of a provider in nestjs.我想模拟一个nest-winston记录器,它是nestjs 中提供程序的依赖项。
@Controller('builder/instance')
export class InstanceController {
private executor: Executor;
constructor(
@Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: Logger,
private stripeService: StripeService,
private instanceService: InstanceService,
private organizationService: OrganizationService,
private executorFactory: ExecutorFactory,
private socketService: SocketService,
private auditLogService: AuditLogService,
) {
this.logger.log("hello world!", InstanceController.name);
this.executor = this.executorFactory.getExecutor();
// ...
}
}
The authors solution was to pass in the loggers token as a provider to the module with an empty useValue.作者的解决方案是将记录器令牌作为提供者传递给具有空 useValue 的模块。 I assume it is because they didn't want to actually call it, but just supply the dependency for one of the providers they mock.我认为这是因为他们不想实际调用它,而只是为他们模拟的提供者之一提供依赖项。
describe('InstanceController', () => {
let controller: InstanceController;
const mockStripeService = {};
const mockInstanceService = {};
const mockOrganizationService = {};
const mockExecutorFactory = {};
const mockSocketService = {};
const mockAuditLogService = {};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
// imports: [AccountModule],
controllers: [InstanceController],
providers: [
{ provide: WINSTON_NEST_MODULE_PROVIDER, useValue: {} },
StripeService,
InstanceService,
OrganizationService,
ExecutorFactory,
SocketService,
AuditLogService,
],
})
.overrideProvider(StripeService)
.useValue(mockStripeService)
.overrideProvider(InstanceService)
.useValue(mockInstanceService)
.overrideProvider(OrganizationService)
.useValue(mockOrganizationService)
.overrideProvider(ExecutorFactory)
.useValue(mockExecutorFactory)
.overrideProvider(SocketService)
.useValue(mockSocketService)
.overrideProvider(AuditLogService)
.useValue(mockAuditLogService)
.compile();
controller = module.get<InstanceController>(InstanceController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});
However, when trying to run the tests, it tries to find this.logger.log
and when this.logger
returns as {}
it says this.logger.log is not a function
.但是,在尝试运行测试时,它会尝试找到this.logger.log
并且当this.logger
返回为{}
时,它会说this.logger.log is not a function
。 Does anyone know of a way to properly mock the logger dependency and can help me with this?有谁知道正确模拟记录器依赖项的方法并可以帮助我解决这个问题? The auto mocking described in nestjs documentation did not work for me either. Nestjs 文档中描述的自动 mocking对我也不起作用。
I am very new with jest and nestjs, and my plaster solution thusfar is to replace the empty useValue with我对 jest 和 nestjs 很陌生,到目前为止,我的石膏解决方案是将空的 useValue 替换为
{
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
}
, which is just... awful. ,这真是……太可怕了。 Help will be greatly appreciated!帮助将不胜感激!
First off, I would suggest not setting every provider and then using override
provider.首先,我建议不要设置每个提供程序,然后使用override
提供程序。 Instead, use a simple custom provider like相反,请使用简单的自定义提供程序,例如
{
provide: StripeService,
useValue: mockStripeService
}
Much less boilerplate when you have this many services.当您拥有这么多服务时,样板文件就会少得多。 I keep the override*()
methods for my integration and e2e tests where I need to override a provider that is registered by another module.我为我的集成和 e2e 测试保留了override*()
方法,我需要覆盖另一个模块注册的提供程序。
Second, if all you end up using in your method is this.logger.log
then mockign log
to jest.fn()
is a fine approach.其次,如果你最终在你的方法中使用的是this.logger.log
,那么模拟log
到jest.fn()
是一个很好的方法。
{
provide: WINSTON_NEST_MODULE_PROVIDER,
useValue: { log: jest.fn() }
}
That will allow your code to run and nothing to log out to the console from the logger.这将允许您的代码运行,并且没有任何内容可以从记录器注销到控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.