Skip to content

Commit

Permalink
fix: Mock responses should fire when timers are mocked (#1336)
Browse files Browse the repository at this point in the history
Fix picked from #1335.

Revert #1270. Fix #1334.
  • Loading branch information
Chengxuan authored and paulmelnikow committed Jan 3, 2019
1 parent 556bb0d commit a213169
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/intercept.js
Expand Up @@ -15,6 +15,7 @@ const _ = require('lodash')
const debug = require('debug')('nock.intercept')
const EventEmitter = require('events').EventEmitter
const globalEmitter = require('./global_emitter')
const timers = require('timers')

/**
* @name NetConnectNotAllowedError
Expand Down Expand Up @@ -279,7 +280,7 @@ function overrideClientRequest() {
if (isOff() || isEnabledForNetConnect(options)) {
originalClientRequest.apply(this, arguments)
} else {
setImmediate(
timers.setImmediate(
function() {
const error = new NetConnectNotAllowedError(
options.host,
Expand Down
11 changes: 6 additions & 5 deletions lib/request_overrider.js
Expand Up @@ -13,6 +13,7 @@ const debug = require('debug')('nock.request_overrider')
const ReadableStream = require('stream').Readable
const globalEmitter = require('./global_emitter')
const zlib = require('zlib')
const timers = require('timers')

function getHeader(request, name) {
if (!request._headers) {
Expand Down Expand Up @@ -41,7 +42,7 @@ function setHeader(request, name, value) {
}

if (name == 'expect' && value == '100-continue') {
setImmediate(function() {
timers.setImmediate(function() {
debug('continue')
request.emit('continue')
})
Expand Down Expand Up @@ -142,7 +143,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
emitError(new Error('Request aborted'))
}

setImmediate(function() {
timers.setImmediate(function() {
req.emit('drain')
})

Expand Down Expand Up @@ -301,7 +302,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
} else {
error = new Error(interceptor.errorMessage)
}
setTimeout(emitError, interceptor.getTotalDelay(), error)
timers.setTimeout(emitError, interceptor.getTotalDelay(), error)
return
}
response.statusCode = Number(interceptor.statusCode) || 200
Expand Down Expand Up @@ -579,13 +580,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}

// Stream the response chunks one at a time.
setImmediate(function emitChunk() {
timers.setImmediate(function emitChunk() {
const chunk = responseBuffers.shift()

if (chunk) {
debug('emitting response chunk')
response.push(chunk)
setImmediate(emitChunk)
timers.setImmediate(emitChunk)
} else {
debug('ending response stream')
response.push(null)
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -46,6 +46,7 @@
"got": "^9.4.0",
"hyperquest": "^2.1.3",
"isomorphic-fetch": "^2.2.0",
"lolex": "^3.0.0",
"markdown-toc": "^1.2.0",
"needle": "^2.2.2",
"nyc": "^12.0.1",
Expand Down
23 changes: 23 additions & 0 deletions tests/test_fake_timer.js
@@ -0,0 +1,23 @@
'use strict'

const nock = require('../.')
const test = require('tap').test
const request = require('request')
const lolex = require('lolex')

// https://github.com/nock/nock/issues/1334
test('one function returns successfully when fake timer is enabled', function(t) {
const clock = lolex.install()
nock('http://www.google.com')
.get('/')
.reply(200)

request.get('http://www.google.com', function(err, resp) {
clock.uninstall()
if (err) {
throw err
}
t.equal(resp.statusCode, 200)
t.end()
})
})

0 comments on commit a213169

Please sign in to comment.