Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I ran into an error when was trying to add some tests to my NestJs App. There is auto generated test file named app.controller.spec.ts which is a unit test. When i try to run tests with the yarn test command it throws an Error stating:

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)

My tsconfig configuration:

{
  "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",
  ]
}

yarn test command: "test": "jest"

Content of the unit test file:

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');
    });
  });
});

like image 580
Mike Shum Avatar asked Oct 24 '25 04:10

Mike Shum


1 Answers

You have @nestjs/testing on version 8, but you're using @nestjs/common, @nestjs/core, and many other @nestjs/ packages down on version 6. These major versions should match each other. Either upgrade everything up to v8, or downgrade @nestjs/testing to v6 to match the common and core packages.

like image 102
Jay McDoniel Avatar answered Oct 26 '25 18:10

Jay McDoniel