为什么 JEST 在使用不同 Syntex 时抛出超时 - 异步回调?

问题描述 投票:0回答:1

我正在 Node.js 中使用 Jest 进行学习,在找到 5000 毫秒超时的修复后遇到了一些问题。奇怪的是,它适用于下面的 Syntex,但不适用于通常在测试用例中使用的更好的 Syntex。

// This works
test('It should respond with 200 success', async () => {
    const response = await request(App).get('/launches'); 
    expect(response.statusCode).toBe(200);
});


// This does not work
test('It should respond with 200 success', async () => {
    const response = await request(App)
        .get('/launches')
        .expect('Content-Type', /json/) 
        .expect(200);
});

我尝试了什么?

我尝试如下...

//jest config file
module.exports = {
  testEnvironment: "node",
};

//Mongoose DB connection config modification in server.js file
mongoose.set("strictQuery", false);

上述解决方案适用于第一个测试用例。但它不适用于第二个。请查找下面的笑话错误,以便更好地理解第二个测试用例。

版本规格: 节点:12.19.0 开玩笑:26.6.3 超级测试:6.1.3 猫鼬:6.3.8

node.js jestjs
1个回答
0
投票

显然,经过一番尝试和错误,我找到了解决方案,只需增加测试函数的超时时间即可。就我而言,将其设置为 6000ms 可以解决问题。可能的原因是与 MongoDB Atlas 的连接速度较慢。超时总是会改变。写在这里,希望能帮助到其他遇到类似问题的人。

test('It should respond with 200 success', async () => {
    const response = await request(App)
        .get('/launches')
        .expect('Content-Type', /json/) 
        .expect(200);
}, 6000); 

如果需要,请随时添加您的输入或修改,谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.