【问题标题】:Supertest + express setup cause timeout errorSupertest + express setup 导致超时错误
【发布时间】:2021-11-22 22:35:58
【问题描述】:

我在 server.test.js 上有一个简单的设置

import 'regenerator-runtime/runtime';

const request = require('supertest');
const express = require("express")
const app = express();

app.get('/user', function(req, res) {
  res.status(200).json({ name: 'john' });
});

describe('GET /user', function() {
  it('responds with json', async function(done) {
    const response = await request(app)
      .get('/user')
    expect(response.status).toBe(201)
  })
})

npx jest 和接收 thrown: "Exceeded timeout of 5000 ms for a test., 增加超时在 jest.config.js 中对 testTimeout: 20000 没有帮助,它会等待 20 秒并且也会失败

为什么 express 应用以超集开头?我使用了官方 README 中的示例

【问题讨论】:

    标签: javascript node.js express jestjs supertest


    【解决方案1】:

    尝试从测试回调中删除done

    it('responds with json', async function() {
      const response = await request(app)
        .get('/user')
      expect(response.status).toBe(201)
    })
    

    Jest 可能正在等待它被调用。

    通常建议您使用async 函数或done。如果你混合它们,Jest 通常会发出警告,不知道为什么它不这样做。

    您也可以尝试在测试结束时致电done()

    您应该期望测试失败,因为发布的状态是 200,而您正在检查 201

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多