NestJs + TypeScript + Jest - TypeError: Class extends value undefined is not a constructor or null

NestJs + TypeScript + Jest - TypeError: Class extends value undefined is not a constructor or null

我 运行 在尝试向我的 NestJs 应用程序添加一些测试时遇到错误。 有一个名为 app.controller.spec.ts 的自动生成的测试文件,它是一个单元测试。当我尝试使用 yarn test 命令进行 运行 测试时,它抛出一个错误说明:

Test suite failed to run TypeError: Class extends value undefined is not a constructor or null

 at Object.<anonymous> (../node_modules/@nestjs/testing/services/testing-logger.service.js:7:38)
 at Object.<anonymous> (../node_modules/@nestjs/testing/testing-module.builder.js:9:34)

我的 tsconfig 配置:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules",
    "./node_modules",
    "./node_modules/*",
    "./node_modules/@types/node/index.d.ts",
  ]
}

纱线测试命令: "test": "jest"

单元测试文件的内容:

import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import {ConfigModule} from './config/config.module';

describe('AppController', () => {
  let appController: AppController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      imports: [ConfigModule],
      controllers: [AppController],
      providers: [AppService],
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should return "pong"', () => {
      expect(appController.getHello()).toBe('pong');
    });
  });
});

您在版本 8 上有 @nestjs/testing,但您在版本 6 上使用 @nestjs/common@nestjs/core 和许多其他 @nestjs/ 软件包。这些主要版本应该相互匹配。要么将所有内容升级到 v8,要么将 @nestjs/testing 降级到 v6 以匹配通用和核心包。