nodejs 测试在 CircleCI 上随机失败



我们正在使用nodejs构建一个应用程序,并使用Jest为其编写异步测试。在测试方面,我们已经面临一段时间的问题;有时测试在 CircleCI 上失败,而有时它们通过。是的,我们使用async/await方法在执行断言之前等待首先获取数据。以下是测试文件中的一个片段:

it('should return the expected number of trips', async () => {
const postResp = await request(app)
.post('/api/v1/requests')
.set('authorization', token)
.send(mockRequest);
const createdRequestId = postResp.body.request.id;
const getResp = await request(app)
.get(`/api/v1/requests/${createdRequestId}`)
.set('authorization', token);
expect(getResp.body.requestData.trips).toHaveLength(2);
});

这是 CircleCI 针对此测试块抛出的错误:TypeError: Cannot read property 'trips' of undefined

必须不止这个错误。 即一些失败的请求或您从服务器返回的响应内容。

您可能想做的是先记录getResp或getResp.body的内容。 然后检查您发出的请求是否在 Circle CI 中返回正确的结果。

您可以通过 ssh 登录到 circle ci,以便您可以按照此处列出的此步骤进行调试。 https://circleci.com/docs/2.0/ssh-access-jobs/取决于您使用的是圆 ci 1 还是 2。

只有这样,您才能知道错误的原因

最新更新