Skip to content

Commit c7363e5

Browse files
salmanmpaulmelnikow
authored andcommitted
fix: Fix a regression due to Jest having different globals (#1850)
Fix a regression in 11.7.1 due to Jest having different globals: jestjs/jest#2549. Check functions using typeof instead of instanceof.
1 parent fad405a commit c7363e5

File tree

7 files changed

+26
-22
lines changed

7 files changed

+26
-22
lines changed

lib/back.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function Back(fixtureName, options, nockedFn) {
7171
} else if (arguments.length === 2) {
7272
// If 2nd parameter is a function then `options` has been omitted
7373
// otherwise `options` haven't been omitted but `nockedFn` was.
74-
if (options instanceof Function) {
74+
if (typeof options === 'function') {
7575
nockedFn = options
7676
options = {}
7777
}
@@ -89,7 +89,7 @@ function Back(fixtureName, options, nockedFn) {
8989
debug('context:', context)
9090

9191
// If nockedFn is a function then invoke it, otherwise return a promise resolving to nockDone.
92-
if (nockedFn instanceof Function) {
92+
if (typeof nockedFn === 'function') {
9393
nockedFn.call(context, nockDone)
9494
} else {
9595
return Promise.resolve({ nockDone, context })
@@ -170,7 +170,7 @@ const record = {
170170
if (context.isRecording) {
171171
let outputs = recorder.outputs()
172172

173-
if (options.afterRecord instanceof Function) {
173+
if (typeof options.afterRecord === 'function') {
174174
outputs = options.afterRecord(outputs)
175175
}
176176

lib/common.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ const noDuplicatesHeaders = new Set([
315315
*/
316316
function addHeaderLine(headers, name, value) {
317317
let values // code below expects `values` to be an array of strings
318-
if (value instanceof Function) {
318+
if (typeof value === 'function') {
319319
// Function values are evaluated towards the end of the response, before that we use a placeholder
320320
// string just to designate that the header exists. Useful when `Content-Type` is set with a function.
321321
values = [value.name]
@@ -476,7 +476,7 @@ function isStream(obj) {
476476
obj &&
477477
typeof obj !== 'string' &&
478478
!Buffer.isBuffer(obj) &&
479-
obj.setEncoding instanceof Function
479+
typeof obj.setEncoding === 'function'
480480
)
481481
}
482482

@@ -500,7 +500,7 @@ function normalizeClientRequestArgs(input, options, cb) {
500500
input = null
501501
}
502502

503-
if (options instanceof Function) {
503+
if (typeof options === 'function') {
504504
cb = options
505505
options = input || {}
506506
} else {

lib/intercepted_request_router.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ class InterceptedRequestRouter {
113113
}
114114
this.requestBodyBuffers.push(buffer)
115115
}
116-
if (callback instanceof Function) {
116+
// can't use instanceof Function because some test runners
117+
// run tests in vm.runInNewContext where Function is not same
118+
// as that in the current context
119+
// https://github.com/nock/nock/pull/1754#issuecomment-571531407
120+
if (typeof callback === 'function') {
117121
callback()
118122
}
119123
} else {
@@ -131,17 +135,17 @@ class InterceptedRequestRouter {
131135
debug('req.end')
132136
const { req } = this
133137

134-
if (chunk instanceof Function) {
138+
if (typeof chunk === 'function') {
135139
callback = chunk
136140
chunk = null
137-
} else if (encoding instanceof Function) {
141+
} else if (typeof encoding === 'function') {
138142
callback = encoding
139143
encoding = null
140144
}
141145

142146
if (!req.aborted && !this.playbackStarted) {
143147
req.write(chunk, encoding, () => {
144-
if (callback instanceof Function) {
148+
if (typeof callback === 'function') {
145149
callback()
146150
}
147151
this.startPlayback()

lib/interceptor.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ module.exports = class Interceptor {
110110

111111
reply(statusCode, body, rawHeaders) {
112112
// support the format of only passing in a callback
113-
if (statusCode instanceof Function) {
113+
if (typeof statusCode === 'function') {
114114
if (arguments.length > 1) {
115115
// It's not very Javascript-y to throw an error for extra args to a function, but because
116116
// of legacy behavior, this error was added to reduce confusion for those migrating.
@@ -126,7 +126,7 @@ module.exports = class Interceptor {
126126
}
127127

128128
this.statusCode = statusCode || 200
129-
if (body instanceof Function) {
129+
if (typeof body === 'function') {
130130
this.replyFunction = body
131131
body = null
132132
}
@@ -215,7 +215,7 @@ module.exports = class Interceptor {
215215
}
216216

217217
if (reqHeader !== undefined && header !== undefined) {
218-
if (reqHeader instanceof Function) {
218+
if (typeof reqHeader === 'function') {
219219
return reqHeader(header)
220220
} else if (common.matchStringOrRegexp(header, reqHeader)) {
221221
return true
@@ -250,7 +250,7 @@ module.exports = class Interceptor {
250250

251251
const requestMatchesFilter = ({ name, value: predicate }) => {
252252
const headerValue = req.getHeader(name)
253-
if (predicate instanceof Function) {
253+
if (typeof predicate === 'function') {
254254
return predicate(headerValue)
255255
} else {
256256
return common.matchStringOrRegexp(headerValue, predicate)
@@ -317,7 +317,7 @@ module.exports = class Interceptor {
317317
matchKey = common.normalizeOrigin(proto, options.host, options.port)
318318
}
319319

320-
if (this.uri instanceof Function) {
320+
if (typeof this.uri === 'function') {
321321
matches =
322322
common.matchStringOrRegexp(matchKey, this.basePath) &&
323323
// This is a false positive, as `uri` is not bound to `this`.
@@ -397,7 +397,7 @@ module.exports = class Interceptor {
397397
debug('Interceptor queries: %j', this.queries)
398398
debug(' Request queries: %j', reqQueries)
399399

400-
if (this.queries instanceof Function) {
400+
if (typeof this.queries === 'function') {
401401
return this.queries(reqQueries)
402402
}
403403

@@ -458,7 +458,7 @@ module.exports = class Interceptor {
458458
return this
459459
}
460460

461-
if (queries instanceof Function) {
461+
if (typeof queries === 'function') {
462462
this.queries = queries
463463
return this
464464
}

lib/match_body.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function matchBody(options, spec, body) {
2626

2727
// try to transform body to json
2828
let json
29-
if (typeof spec === 'object' || spec instanceof Function) {
29+
if (typeof spec === 'object' || typeof spec === 'function') {
3030
try {
3131
json = JSON.parse(body)
3232
} catch (err) {
@@ -39,7 +39,7 @@ module.exports = function matchBody(options, spec, body) {
3939
}
4040
}
4141

42-
if (spec instanceof Function) {
42+
if (typeof spec === 'function') {
4343
return spec.call(options, body)
4444
}
4545

lib/playback_interceptor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ function playbackInterceptor({
270270

271271
// Evaluate functional headers.
272272
common.forEachHeader(response.rawHeaders, (value, fieldName, i) => {
273-
if (value instanceof Function) {
273+
if (typeof value === 'function') {
274274
response.rawHeaders[i + 1] = value(req, response, responseBody)
275275
}
276276
})

lib/recorder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ function record(recOptions) {
344344
const oldEnd = req.end
345345
req.end = function(chunk, encoding, callback) {
346346
debug('req.end')
347-
if (chunk instanceof Function) {
347+
if (typeof chunk === 'function') {
348348
callback = chunk
349349
chunk = null
350-
} else if (encoding instanceof Function) {
350+
} else if (typeof encoding === 'function') {
351351
callback = encoding
352352
encoding = null
353353
}

0 commit comments

Comments
 (0)