4

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

5
  • What are you @nestjs/ package versions? Can you add those to your question? Commented Aug 18, 2021 at 15:50
  • Thank you for your reply! Here they are: "@nestjs/common": "6.7.2", "@nestjs/core": "6.7.2", "@nestjs/jwt": "6.1.1", "@nestjs/mongoose": "6.1.2", "@nestjs/passport": "6.1.0", "@nestjs/platform-express": "6.7.2", "@nestjs/typeorm": "6.2.0", "jest": "24.9.0" NodeJs version: v14.17.4 Commented Aug 19, 2021 at 3:33
  • Furthermore, utils that i used to find circular dependencies (first, i thought it was causing the problem) say there's no circular dependencies in the project. Commented Aug 19, 2021 at 3:48
  • Oh wow, back on Nest v6. What about your dev deps for @nestjs/ as well Commented Aug 19, 2021 at 4:20
  • "@nestjs/cli": "6.9.0", "@nestjs/schematics": "6.7.0", "@nestjs/testing": "^8.0.6" I really appreciate your feedback, yet i suppose the question is not relevant for me anymore. As we had no time for searching for a solution, we decided just to write API tests with postman. We can still work around it, so it would help others who is facing a similar problem. Commented Aug 19, 2021 at 5:49

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.