如何使用nock
(https://github.com/node-nock/nock)强制 ETIMEDOUT 和request
?
我尝试了以下操作,其中nock
将延迟响应 8000 毫秒,而请求超时将设置为 5000 毫秒,所以我希望看到ETIMEDOUT
但事实并非如此。代码将返回then block should not execute: Hello from Google!
摩卡测试/force-req-timeout.test.js --timeout=10000
'use strict'
const expect = require('chai').expect
const nock = require('nock')
const rp = require('request-promise')
describe('force request timeout with nock', () => {
it('should return ETIMEDOUT', (done) => {
nock('http://www.google.com')
.get('/')
.delay(8000)
.reply(200, 'Hello from Google!')
rp({
url: 'http://www.google.com',
timeout: 5000
})
.then((data) => {
console.log('then block should not execute: ', data)
})
.catch((err) => {
expect(err.cause.code).to.equal('ETIMEDOUT')
return done()
})
})
})