Jest & Supertest 等待没有回应



我已经编写了以下测试用例:

test("should respond with a 200 tatus code",async () => {
const response = await request(users).post("/signup").send({"email":"wwww12dSD3","password":"adasdasdas","username":"adasdasdasdaYrrrrY"})
expect(response.statusCode).toBe(200)
})

对应节点js路由为

router.route('/signup').post(jsonParser,async (req, res) => {
//create new User
//Turn email to lowercase since email is case insensitive
console.log(req.body);
const email = req.body.email.toLowerCase();
const password = await bcrypt.hash(req.body.password, 10);
const username = req.body.username;

//check if user already exists
const oldUser = User.find({email: email});
if ((await oldUser).length >= 1){
res.status(400).json('User already exists');
} else {
//save new user
const newUser = new User({email, password, username});
console.log(newUser);
newUser.save()
.then(() => res.status(200).json('Sign up successful'))
.catch(err => res.status(500).json('Error: ' + err));
}
});

我收到一个错误

thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

然而,它可以获得reqbody,但不能获得

console.log(newUser);

因此没有发出响应

但当我使用失眠时,我可以得到正确的反应

任何有关这方面的建议都会有所帮助。

您应该在异步测试函数的最后使用done((函数。否则它将永远等待

最新更新