javascript - Nestjs 单元测试中不支持从“描述”返回 Promise
问题描述
我想使用 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
块)需要同步。async
从顶层describe
块中删除。
推荐阅读
- javascript - react native:解决键盘在文本框上方并且看不到正在写什么的问题的方法是什么?
- acumatica - 如何更改选择器描述字段的显示名称
- reactjs - 在外部窗口中渲染 React 组件时传递文档
- java - 运行 java 服务器时获取无效的最大堆大小
- excel - 创建指向特定选项卡 Sharepoint 文档工作簿的链接
- google-bigquery - 使用另一个表生成 CASE WHEN 语句
- ios - Azure AD B2C 的 iOS 14 问题
- excel - VBA 在 word 中查找和替换。用数字和单词之间的不间断空格替换空格
- sql - EF Core 插入操作 - SQL 命令如何工作?
- json - 当值为整数时,不能将小数部分显示为 .0