我不确定当拦截器被调用时,nock是支持等待还是使用回调/事件发射器进行通知。
例如:
const scope = nock(config.research.url)
.post(config.research.routes.getOdds, expectBody)
.reply(200)
while(true){
expect(scope.isDone()).toBeTruthy()
await sleep(500)
}我的问题
如何使用nock接口改进上述代码?
发布于 2021-05-11 18:55:19
nock在请求和响应时发出事件:https://github.com/nock/nock/#events
emit('request', function(req, interceptor, body))
emit('replied', function(req, interceptor))您的代码可以侦听replied事件以获得通知。
发布于 2021-05-11 18:55:54
nock允许向reply函数传递回调,这可以是处理此问题的方法之一。将您的reply代码包装到Promise中并等待它。
另一种解决方案是订阅scope发出的replied事件,并将其包装到Promise中以等待回复
scope.on('replied', function(req, interceptor) { ... })https://stackoverflow.com/questions/67485236
复制相似问题