【发布时间】:2022-01-22 22:59:18
【问题描述】:
错误
throw:“钩子超时超过 5000 毫秒。 如果这是一个长时间运行的测试,请使用 jest.setTimeout(newTimeout) 增加超时值。"
24 | > 25 | afterAll(async () => { | ^ 26 | jest.setTimeout(20000); 27 | await mongo.stop(); 28 | await mongoose.connection.close(); at Object.<anonymous> (src/test/setup.ts:25:1) at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
测试代码
setup.test.ts
import { MongoMemoryServer } from 'mongodb-memory-server';
import mongoose from 'mongoose';
import { app } from '../app';
let mongo: any;
beforeAll(async () => {
jest.setTimeout(10000);
process.env.JWT_KEY = 'asdfasd';
mongo = await MongoMemoryServer.create();
const uri = await mongo.getUri();
await mongoose.connect(uri);
});
beforeEach(async () => {
jest.setTimeout(10000);
const collections = await mongoose.connection.db.collections();
for(let collection of collections){
await collection.deleteMany({});
}
});
afterAll(async () => {
jest.setTimeout(20000);
await mongo.stop();
await mongoose.connection.close();
})
依赖
"mongodb-memory-server": "^8.0.4", "@types/jest": "^27.0.3", “超级测试”:“^6.1.6”,“ts-jest”:“^27.1.2”
【问题讨论】:
标签: typescript mongodb express ts-jest