Reactjs 超时-在jest.setTimeout指定的5000ms超时内未调用异步回调
我正在使用jest测试API的用户功能(注册和登录) 测试代码:Reactjs 超时-在jest.setTimeout指定的5000ms超时内未调用异步回调,reactjs,express,jestjs,supertest,Reactjs,Express,Jestjs,Supertest,我正在使用jest测试API的用户功能(注册和登录) 测试代码: const request = require('supertest'); const app = require('../../app'); describe('Test User Functionality', () => { test('User should be able to login', async done => { const response = await request(ap
const request = require('supertest');
const app = require('../../app');
describe('Test User Functionality', () => {
test('User should be able to login', async done => {
const response = await request(app)
.post('/api/users/login')
.send({
email: 'test@test.com',
password: 'welcome1',
})
.expect(200);
done();
//expect(response.statusCode).toBe(200);
});
test('User should be able to signup', async done => {
const response = await request(app)
.post('/api/users/signup')
.send({
username: 'testuser',
email: 'test@test1.com',
password: 'welcome1',
})
.expect(200);
done();
//expect(response.statusCode).toBe(200);
});
});
在我有一个测试的情况下,它工作得很好,但有多个测试在里面,它显示超时错误
以下是错误的屏幕截图:
我尝试添加超时,交换测试,但仍然没有成功
任何人都请帮忙 在使用异步请求测试react组件时,我遇到了相同的问题 发生这种情况是因为您没有正确完成请求 你可以很容易地解决这个问题 选项1:移动
done
作为expect
函数调用的第二个参数,如下所示
const response = await request(app)
.post('/api/users/signup')
.send({
username: 'testuser',
email: 'test@test1.com',
password: 'welcome1',
})
.expect(200, done);
选项2:使用end
方法
const response = await request(app)
.post('/api/users/signup')
.send({
username: 'testuser',
email: 'test@test1.com',
password: 'welcome1',
})
.expect(200)
.end((err, res) => {
// do anything you want!
})
或者您可以签出文档() 一个可能的问题可能是express Middleware。 要了解这是否是您的情况,您可以:
应用程序)的所有中间件。使用(/*中间件*/)
)library\u name.js
(这是
为开玩笑而手动模拟:
)app.use(Your.Middleware)
并在中间件文件中传递ioredis
,在Redis实例化的文件中,它多次尝试连接到商店,导致超时。事实证明,主机名是错误的。泄露问题的关键是模仿中间件并找到额外的设置。然后Jest显示了另一个提示商店连接问题的错误。您解决过这个问题吗?
/** Possibly imports and some setup here */
// Make sure the function being called is mocked
export default {
Middleware: (req, res, next) => {
next();
},
}