supertest的期望和之后的差异?

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

使用supertest在JavaScript中测试异步HTTP请求时,这两个片段之间的区别是什么?其中一个是正确的而另一个是错的吗?

request('http://localhost:8080/').get('/api/people') .expect(res => res.body.should.have.length(5))

request('http://localhost:8080/').get('/api/people') .then(res => res.body.should.have.length(5))

我能注意到的唯一区别是:

  • expect返回一个Test对象,当测试失败时,打印一个大的堆栈跟踪
  • then返回一个Promise对象,当测试失败时,打印一个小堆栈跟踪
javascript automated-tests es6-promise supertest
1个回答
0
投票

根据您使用的测试运行器显然会影响答案,但像Mocha这样的东西将允许您直接在测试中返回Promise,这将等待测试通过之前解决。

所以如果你有类似的东西:

describe('Your test case', function () {

  it('will wait for promise to resolve', function () {
    return request('http://localhost:8080/').get('/api/people')
      .then(res => res.body.should.have.length(5))
  })

})

而在另一个实例中,你真的应该根据https://www.npmjs.com/package/supertest docs使用完成回调。

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