首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nest.js测试错误:在Nest v8中不允许使用"extends“指令。请用“扩展ConsoleLogger”代替

Nest.js测试错误:在Nest v8中不允许使用"extends“指令。请用“扩展ConsoleLogger”代替
EN

Stack Overflow用户
提问于 2021-08-07 03:53:58
回答 2查看 5.9K关注 0票数 13

我的问题是:

我正在Nest.js中使用我的自定义日志:

代码语言:javascript
运行
复制
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);
  }
}

log.interceptor.ts文件:

代码语言:javascript
运行
复制
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`,
          ),
        ),
      );
  }
}

下面是main.ts文件:

代码语言:javascript
运行
复制
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);
}

当我运行npm run start:dev来运行Nest时,一切都很好。但是,当我在测试中运行npm run test:e2enpm run test时,它会显示以下错误:

代码语言:javascript
运行
复制
  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();

我再次阅读了Nest.js文档,并在文档中找到了测井破断变化。但问题是,我已经让我的ReportLogger扩展了ConsoleLogger,为什么这个错误再次显示出来?为什么它只显示在测试中?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-07 19:13:45

在将NestJS升级到版本8之后,我也遇到了同样的问题。

后来,我发现包@nestjs/testing已经安装了以前的版本,并且没有升级到最新版本。原因是,以前版本的NestJS测试模块使用的是旧的Logger。

为了解决此问题,只需升级NestJS测试模块即可。

运行以下命令以获取最新版本:

代码语言:javascript
运行
复制
npm i @nestjs/testing@latest

特定版本的

代码语言:javascript
运行
复制
npm i @nestjs/testing@8.0.6 // <--- Change the NestJS version here

在此之后,只需再次构建和运行测试用例。

外部链接:

票数 30
EN

Stack Overflow用户

发布于 2021-09-24 19:15:02

即使使用"@nestjs/testing": "^8.0.7",这个问题仍然会发生。

代码语言:javascript
运行
复制
class Logger implements LoggerService { ... }

await Test.createTestingModule({
  imports: [ApiModule],
})
  .setLogger(new Logger())
  .compile();

设置记录器实例解决了我的错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68689281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档