javascript - supertest的expect和then的区别?
问题描述
在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
对象,当测试失败时,打印一个小的堆栈跟踪
解决方案
取决于您使用的测试运行器显然会影响答案,但类似的东西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文档使用 done 回调。
推荐阅读
- java - 春云流兔的退避设置
- css - Bootstrap 4 将图像移出卡片标题的容器
- primefaces - 如何从 Prime Faces 6.2 的数据表中将参数传递给模态窗口?
- reactjs - React-dom 是做什么的
- sql-server - 将行连接成字符串的查询在 Select 语句中有效,但在表值函数中无效。我错过了什么?
- c# - C# 无法检查用户是否存在于 SQL Server 表中
- html - Html:图片上的链接
- npm - module.js 的 npm 错误 549 找不到 npm-cli.js
- python - Django JSONDecodeError at ...= get_response(request)
- java - org.springframework.data.mapping.PropertyReferenceException:没有找到类型客户端的属性保存