33const _ = require ( 'lodash' )
44const debug = require ( 'debug' ) ( 'nock.common' )
55const url = require ( 'url' )
6+ const timers = require ( 'timers' )
67
78/**
89 * Normalizes the request options so that it always has `host` property.
@@ -593,6 +594,32 @@ function deepEqual(expected, actual) {
593594 return expected === actual
594595}
595596
597+ const timeouts = [ ]
598+ const intervals = [ ]
599+ const immediates = [ ]
600+
601+ const wrapTimer = ( timer , ids ) => ( ...args ) => {
602+ const id = timer ( ...args )
603+ ids . push ( id )
604+ return id
605+ }
606+
607+ const setTimeout = wrapTimer ( timers . setTimeout , timeouts )
608+ const setInterval = wrapTimer ( timers . setInterval , intervals )
609+ const setImmediate = wrapTimer ( timers . setImmediate , immediates )
610+
611+ function clearTimer ( clear , ids ) {
612+ while ( ids . length ) {
613+ clear ( ids . shift ( ) )
614+ }
615+ }
616+
617+ function removeAllTimers ( ) {
618+ clearTimer ( clearTimeout , timeouts )
619+ clearTimer ( clearInterval , intervals )
620+ clearTimer ( clearImmediate , immediates )
621+ }
622+
596623exports . normalizeClientRequestArgs = normalizeClientRequestArgs
597624exports . normalizeRequestOptions = normalizeRequestOptions
598625exports . normalizeOrigin = normalizeOrigin
@@ -615,3 +642,7 @@ exports.matchStringOrRegexp = matchStringOrRegexp
615642exports . formatQueryValue = formatQueryValue
616643exports . isStream = isStream
617644exports . dataEqual = dataEqual
645+ exports . setTimeout = setTimeout
646+ exports . setInterval = setInterval
647+ exports . setImmediate = setImmediate
648+ exports . removeAllTimers = removeAllTimers
0 commit comments