0

I'm trying to test modules on *.spec.ts, but i cannot.

written methods after expect always occur TS2239 error(Property ~ does not exist on type 'Assertion')

however, if i tag the @ts-ignore above the line, it successfully compile and test my code.

Thought it was a NestJS bug, but my other NestJS projects run tests well.

The only difference between the malfunctioned project and my other NestJS projects is using a static module to carry the Vue SPA files.

What should I have to check to solve this problem?

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

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

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

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

  describe('root', () => {
    it('should return "Hello World!"', () => {
      // Error here: TS2339: Property 'toBe' does not exist on type 'Assertion'
      // @ts-ignore can solve this problem, but it is not the correct answer i believe.
      expect(appController.getHello()).toBe('Hello World!');
    });
  });
});

Tried to compare difference between the project that works and not.

2 Answers2

0

I found the answer.

Because of the Cypress in Vue, the NestJS test code does not work. (my NestJS project contains the Vue project named as 'webapp')

Cypress causing type errors in jest assertions

  • Answers containing links should include the information in the answer so that this answer will still be useful if the link breaks – Harrison Jul 27 '23 at 20:06
0

I think you might be experiencing conflicts due to using Cypress in the same place as the backend. Would it not be better to isolate the frontend from the backend? This way, you could use the NestJS testing features without conflicting with the Cypress configurations, etc... In my opinion, that would be better.

Another thing you could check in this project is whether you have @types/jest installed.