Reputation: 159
Here's the problem I have:
I am using my custom Logger in Nest.js:
export class ReportLogger extends ConsoleLogger {
verbose(message: string) {
console.log('【Verbose】Reporting', message);
super.verbose.apply(this, arguments);
}
log(message: string) {
console.log('【Log】Reporting', message);
super.log.apply(this, arguments);
}
}
And the log.interceptor.ts
file:
export class LogInterceptor implements NestInterceptor {
constructor(private reportLogger: ReportLogger) {
this.reportLogger.setContext('LogInterceptor');
}
intercept(context: ExecutionContext, next: CallHandler) {
const http = context.switchToHttp();
const request = http.getRequest();
const now = Date.now();
return next
.handle()
.pipe(
tap(() =>
this.reportLogger.log(
`${request.method} ${request.url} ${Date.now() - now}ms`,
),
),
);
}
}
And here's the main.ts
file:
async function bootstrap() {
const reportLogger = new ReportLogger();
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: {
origin: ['http://localhost', 'http://localhost:3000'],
credentials: true,
},
bufferLogs: true,
logger: reportLogger,
});
app.useGlobalInterceptors(
new LogInterceptor(reportLogger),
);
setupSwagger(app);
await app.listen(4200);
}
When I run npm run start:dev
to run the Nest App on dev, everything works fine. But when I run npm run test:e2e
or npm run test
on testing, it shows this error:
Using the "extends Logger" instruction is not allowed in Nest v8. Please, use "extends ConsoleLogger" instead.
10 | const moduleFixture: TestingModule = await Test.createTestingModule({
11 | imports: [AppModule],
> 12 | }).compile();
| ^
13 |
14 | app = moduleFixture.createNestApplication();
15 | await app.init();
I read the Nest.js doc again, and found the Logging breaking change in the docs. But the question is I have already made my ReportLogger extends ConsoleLogger, why this error shows again? And why it only shows in testing?
Upvotes: 14
Views: 8522
Reputation: 81
even with "@nestjs/testing": "^8.0.7"
this issue still occurs
class Logger implements LoggerService { ... }
await Test.createTestingModule({
imports: [ApiModule],
})
.setLogger(new Logger())
.compile();
setting logger instance solves that error on my side
Upvotes: 0
Reputation: 777
I have faced the same problem after upgrading NestJS to version 8.
Later on, I found that package @nestjs/testing
had previous version installed and was not upgraded to the latest version. The reason is, previous version of NestJS testing module is using the old Logger.
In order to fix this issue, you just need to upgrade the NestJS testing module.
Run this command for Latest version:
npm i @nestjs/testing@latest
OR specific version
npm i @nestjs/[email protected] // <--- Change the NestJS version here
After this, just build and run test cases again.
External Links:
Upvotes: 33