我正在尝试使用 supertest 通过 Jest 检查 res.body,但以下代码段将始终失败
request(app)
.post('/auth/signup')
.send(validEmailSample)
.expect(200, {
success: true,
message: 'registration success',
token: expect.any(String),
user: expect.any(Object),
});
Run Code Online (Sandbox Code Playgroud)
但是当我重写测试以检查回调中的主体时,如下所示:
test('valid userData + valid email will result in registration sucess(200) with message object.', (done) => {
request(app)
.post('/auth/signup')
.send(validEmailSample)
.expect(200)
.end((err, res) => {
if (err) done(err);
expect(res.body.success).toEqual(true);
expect(res.body.message).toEqual('registration successful');
expect(res.body.token).toEqual(expect.any(String));
expect(res.body.user).toEqual(expect.any(Object));
expect.assertions(4);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
测试将通过。
我确定这与expect.any(). 正如 Jest 的文档所说,expect.any 和 expect.anything 只能与 一起使用expect().toEqual,expect().toHaveBeenCalledWith()
我想知道是否有更好的方法来做到这一点,在 supertest 的 expect api 中使用 expect.any。