Skip to content

Commit

Permalink
fix: Fix a regression due to Jest having different globals (#1850)
Browse files Browse the repository at this point in the history
Fix a regression in 11.7.1 due to Jest having different globals: jestjs/jest#2549. Check functions using typeof instead of instanceof.
  • Loading branch information
salmanm authored and paulmelnikow committed Jan 8, 2020
1 parent fad405a commit c7363e5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/back.js
Expand Up @@ -71,7 +71,7 @@ function Back(fixtureName, options, nockedFn) {
} else if (arguments.length === 2) {
// If 2nd parameter is a function then `options` has been omitted
// otherwise `options` haven't been omitted but `nockedFn` was.
if (options instanceof Function) {
if (typeof options === 'function') {
nockedFn = options
options = {}
}
Expand All @@ -89,7 +89,7 @@ function Back(fixtureName, options, nockedFn) {
debug('context:', context)

// If nockedFn is a function then invoke it, otherwise return a promise resolving to nockDone.
if (nockedFn instanceof Function) {
if (typeof nockedFn === 'function') {
nockedFn.call(context, nockDone)
} else {
return Promise.resolve({ nockDone, context })
Expand Down Expand Up @@ -170,7 +170,7 @@ const record = {
if (context.isRecording) {
let outputs = recorder.outputs()

if (options.afterRecord instanceof Function) {
if (typeof options.afterRecord === 'function') {
outputs = options.afterRecord(outputs)
}

Expand Down
6 changes: 3 additions & 3 deletions lib/common.js
Expand Up @@ -315,7 +315,7 @@ const noDuplicatesHeaders = new Set([
*/
function addHeaderLine(headers, name, value) {
let values // code below expects `values` to be an array of strings
if (value instanceof Function) {
if (typeof value === 'function') {
// Function values are evaluated towards the end of the response, before that we use a placeholder
// string just to designate that the header exists. Useful when `Content-Type` is set with a function.
values = [value.name]
Expand Down Expand Up @@ -476,7 +476,7 @@ function isStream(obj) {
obj &&
typeof obj !== 'string' &&
!Buffer.isBuffer(obj) &&
obj.setEncoding instanceof Function
typeof obj.setEncoding === 'function'
)
}

Expand All @@ -500,7 +500,7 @@ function normalizeClientRequestArgs(input, options, cb) {
input = null
}

if (options instanceof Function) {
if (typeof options === 'function') {
cb = options
options = input || {}
} else {
Expand Down
12 changes: 8 additions & 4 deletions lib/intercepted_request_router.js
Expand Up @@ -113,7 +113,11 @@ class InterceptedRequestRouter {
}
this.requestBodyBuffers.push(buffer)
}
if (callback instanceof Function) {
// can't use instanceof Function because some test runners
// run tests in vm.runInNewContext where Function is not same
// as that in the current context
// https://github.com/nock/nock/pull/1754#issuecomment-571531407
if (typeof callback === 'function') {
callback()
}
} else {
Expand All @@ -131,17 +135,17 @@ class InterceptedRequestRouter {
debug('req.end')
const { req } = this

if (chunk instanceof Function) {
if (typeof chunk === 'function') {
callback = chunk
chunk = null
} else if (encoding instanceof Function) {
} else if (typeof encoding === 'function') {
callback = encoding
encoding = null
}

if (!req.aborted && !this.playbackStarted) {
req.write(chunk, encoding, () => {
if (callback instanceof Function) {
if (typeof callback === 'function') {
callback()
}
this.startPlayback()
Expand Down
14 changes: 7 additions & 7 deletions lib/interceptor.js
Expand Up @@ -110,7 +110,7 @@ module.exports = class Interceptor {

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

this.statusCode = statusCode || 200
if (body instanceof Function) {
if (typeof body === 'function') {
this.replyFunction = body
body = null
}
Expand Down Expand Up @@ -215,7 +215,7 @@ module.exports = class Interceptor {
}

if (reqHeader !== undefined && header !== undefined) {
if (reqHeader instanceof Function) {
if (typeof reqHeader === 'function') {
return reqHeader(header)
} else if (common.matchStringOrRegexp(header, reqHeader)) {
return true
Expand Down Expand Up @@ -250,7 +250,7 @@ module.exports = class Interceptor {

const requestMatchesFilter = ({ name, value: predicate }) => {
const headerValue = req.getHeader(name)
if (predicate instanceof Function) {
if (typeof predicate === 'function') {
return predicate(headerValue)
} else {
return common.matchStringOrRegexp(headerValue, predicate)
Expand Down Expand Up @@ -317,7 +317,7 @@ module.exports = class Interceptor {
matchKey = common.normalizeOrigin(proto, options.host, options.port)
}

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

if (this.queries instanceof Function) {
if (typeof this.queries === 'function') {
return this.queries(reqQueries)
}

Expand Down Expand Up @@ -458,7 +458,7 @@ module.exports = class Interceptor {
return this
}

if (queries instanceof Function) {
if (typeof queries === 'function') {
this.queries = queries
return this
}
Expand Down
4 changes: 2 additions & 2 deletions lib/match_body.js
Expand Up @@ -26,7 +26,7 @@ module.exports = function matchBody(options, spec, body) {

// try to transform body to json
let json
if (typeof spec === 'object' || spec instanceof Function) {
if (typeof spec === 'object' || typeof spec === 'function') {
try {
json = JSON.parse(body)
} catch (err) {
Expand All @@ -39,7 +39,7 @@ module.exports = function matchBody(options, spec, body) {
}
}

if (spec instanceof Function) {
if (typeof spec === 'function') {
return spec.call(options, body)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/playback_interceptor.js
Expand Up @@ -270,7 +270,7 @@ function playbackInterceptor({

// Evaluate functional headers.
common.forEachHeader(response.rawHeaders, (value, fieldName, i) => {
if (value instanceof Function) {
if (typeof value === 'function') {
response.rawHeaders[i + 1] = value(req, response, responseBody)
}
})
Expand Down
4 changes: 2 additions & 2 deletions lib/recorder.js
Expand Up @@ -344,10 +344,10 @@ function record(recOptions) {
const oldEnd = req.end
req.end = function(chunk, encoding, callback) {
debug('req.end')
if (chunk instanceof Function) {
if (typeof chunk === 'function') {
callback = chunk
chunk = null
} else if (encoding instanceof Function) {
} else if (typeof encoding === 'function') {
callback = encoding
encoding = null
}
Expand Down

0 comments on commit c7363e5

Please sign in to comment.