簡體   English   中英

Nestjs 單元測試中不支持從“describe”返回 Promise

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

我想使用 mongoose 對 Nestjs 中的服務進行單元測試。 最后出現這個錯誤

不支持從“描述”返回 Promise。 測試必須同步定義。

從“describe”返回一個值將在 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 (不是 Nest)不支持從describe回調返回 promise。 您可以進行異步測試,但測試套件( describe塊)需要同步。 從頂層describe塊中刪除async

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM