我有一个日志服务
import { Logger } from '@nestjs/common';
import { RequestIdProvider } from "src/common/services/request-id.provider";
export class AppLogger extends Logger {
constructor(private readonly requestIdProvider: RequestIdProvider) {
super();
}
private formatMessage(message: any[], requestId: string): string {
const logMessage = message.map((msg) => (typeof msg === 'string' ? msg : JSON.stringify(msg))).join(' ');
return `[Request ID: ${requestId}] ${logMessage}`;
}
log(...messages: any[]) {
const requestId = this.requestIdProvider.getRequestId();
super.log(this.formatMessage(messages, requestId));
}
这里是request-id提供程序
import { REQUEST } from "@nestjs/core";
import { Injectable, Scope, Inject } from '@nestjs/common';
@Injectable({ scope: Scope.REQUEST })
export class RequestIdProvider {
constructor(@Inject(REQUEST) private readonly request) { }
getRequestId(): string {
return this.request.requestId;
}
}
在我的测试案例中
describe('MyController', () => {
let controller: MyController;
let service: myService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [RolesController],
providers: [
MyService, AppLogger, RequestIdProvider,
但我正在面对
● MyController › update › should update a role
TypeError: Cannot read property 'getRequestId' of undefined
1条答案
按热度按时间6yoyoihd1#
我必须创建appLogger示例
然后在供应商