【问题标题】:Test Suite Failed to run测试套件无法运行
【发布时间】: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


    【解决方案1】:

    超时是指测试时间超过 5000 毫秒。

    您可以以编程方式设置测试超时,尽管我认为这不能在测试中完成(如上所示),它需要在像 jest.setup.js 这样的全局设置文件中完成

    或者,我建议在您的 jest.config.js 中设置超时

    示例:

    {
      "name": "my-project",
      "jest": {
        "verbose": true,
        "testTimeout": 5000
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 2022-12-22
      • 2019-10-21
      • 2020-11-25
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多