简体   繁体   中英

Returning a Promise from “describe” is not supported In Nestjs Unit testing

I want to do unit testing of service in nestjs using mongoose. This error comes up at the end

Returning a Promise from "describe" is not supported. Tests must be defined synchronously.

Returning a value from "describe" will fail the test in a future version of Jest.

在此处输入图像描述

import { Test, TestingModule } from '@nestjs/testing';
import { MongooseModule } from '@nestjs/mongoose';

import { closeInMongodConnection, rootMongooseTestModule } from '../test-utils/mongo/MongooseTestModule';
import { UserSchema } from './user.model';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

describe('UserController', async () => {
  let controller: UsersController;
  let service: UsersService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [rootMongooseTestModule(), MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])],
      controllers: [UsersController],
      providers: [UsersService],
    }).compile();

    controller = module.get<UsersController>(UsersController);
    service = module.get<UsersService>(UsersService);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
  });

  it('should return all the events', async () => {
    const result = {
      totalDocs: 0,
      resource: [],
    };

    const data = await service.findAll();
    expect(data).toEqual(result);
  });
  

  afterAll(async () => {
    await closeInMongodConnection();
  });
});

Jest (not Nest) doesn't support returning a promise from the describe callback. You can have asynchronous tests, but the test suite (the describe block) needs to be synchronous. Remove the async from your top level describe block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM