4

How to force ETIMEDOUT with nock (https://github.com/node-nock/nock) and request?
I tried following, where nock will delay response for 8000 milliseconds while timeout for request will be set to 5000 milliseconds, so i would expect to see ETIMEDOUT but that is not the case. Code will return then block should not execute: Hello from Google!

mocha test/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()
    })
  })
})
Srle
  • 10,366
  • 8
  • 34
  • 63

1 Answers1

4

In the end i solved it with:

nock('http://www.google.com')
  .get('/')
  .replyWithError({code: 'ETIMEDOUT'})

Which looks clean, it does not involve delay and stuff, and as it seems to me it is enough to simulate ETIMEDOUT having in mind that im using request-promise lib where im checking for ETIMEDOUT like: if (err.error && err.error.code === 'ETIMEDOUT')

Srle
  • 10,366
  • 8
  • 34
  • 63
  • There are two type of timeouts, ETIMEDOUT and ESOCKETTIMEDOUT. To make it more realistic while waiting for the Nock team to have it fixed, add connect:true or connect:false respectively. – Francis Zabala Sep 06 '17 at 10:47