Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only activate once, when on #1824

Merged
merged 4 commits into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions index.js
Expand Up @@ -6,6 +6,7 @@ const {
activate,
isActive,
isDone,
isOn,
pendingMocks,
activeMocks,
removeInterceptor,
Expand All @@ -29,9 +30,9 @@ Object.assign(module.exports, {
disableNetConnect,
enableNetConnect,
// TODO-12.x Historically `nock.cleanAll()` has returned the nock global.
// The other global methods do noto do this, so it's not clear this was
// The other global methods do not do this, so it's not clear this was
// deliberate or is even helpful. This shim is included for backward
// compatibility and shoulud be replaced with an alias to `removeAll()`.
// compatibility and should be replaced with an alias to `removeAll()`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I blame my MacBook keyboard for this? 😆Thanks for the fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in the same boat. Everything I need an "f" there's a 50/50 chance my laptop will explode.

cleanAll() {
removeAll()
return module.exports
Expand All @@ -49,3 +50,8 @@ Object.assign(module.exports, {
restore: recorder.restore,
back,
})

if (isOn()) {
// Setting the Back mode "activates" Nock by overriding the global entries in the `http/s` modules.
back.setMode(process.env.NOCK_BACK_MODE || 'dryrun')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is doing the right thing, though this implementation seems like a smell. I suppose it's because Nock Back is a sort of separable and optional component which has nothing to do getting Nock set up, and here is used for a side effect.

I really appreciate your making this change, though wonder, is it possible to refactor this a bit further so the code here looks more like if isOn() { activate() }? Perhaps Nock Back could be lazily set up, either when it is first used or if setMode() is invoked directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean Back wouldn't be in "dryrun" mode when Nock is imported. What all would that break?
Also if NOCK_BACK_MODE is set, we'd still need the Back call somewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean Back wouldn't be in "dryrun" mode when Nock is imported. What all would that break?

Hmm, that's a good question. Back's behavior is undefined outside of calls to nock.back() though devs are probably used to today's behavior, which is that NOCK_BACK_MODE is honored. I guess that would have to be dealt with in a breaking change.

Okay, I guess it's fine for now to do this, though leet's add to the comment an explanation that (1) we always activate nock on import, overriding the globals, (2) if nock back is configured, we need to honor that setting for backward compatibility reasons, and (3) otherwise we rely on nock back's default initializing side effect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omg leet's

}
1 change: 0 additions & 1 deletion lib/back.js
Expand Up @@ -281,6 +281,5 @@ Back.setMode = function(mode) {

Back.fixtures = null
Back.currentMode = null
Back.setMode(process.env.NOCK_BACK_MODE || 'dryrun')

module.exports = Back
3 changes: 0 additions & 3 deletions lib/intercept.js
Expand Up @@ -426,8 +426,6 @@ function activate() {
})
}

activate()

module.exports = {
addInterceptor,
remove,
Expand All @@ -441,7 +439,6 @@ module.exports = {
activeMocks,
enableNetConnect,
disableNetConnect,
overrideClientRequest,
restoreOverriddenClientRequest,
abortPendingRequests: common.removeAllTimers,
}
1 change: 1 addition & 0 deletions tests/test_back.js
Expand Up @@ -489,6 +489,7 @@ test('nockBack lockdown tests', nw => {

test('nockBack dryrun throws the expected exception when fs is not available', t => {
const nockBackWithoutFs = proxyquire('../lib/back', { fs: null })
nockBackWithoutFs.setMode('dryrun')

nockBackWithoutFs.fixtures = `${__dirname}/fixtures`
t.throws(() => nockBackWithoutFs('goodRequest.json'), { message: 'no fs' })
Expand Down
12 changes: 12 additions & 0 deletions tests/test_nock_off.js
@@ -1,6 +1,7 @@
'use strict'

const { expect } = require('chai')
const http = require('http')
const nock = require('..')
const got = require('./got_client')
const ssl = require('./ssl')
Expand Down Expand Up @@ -41,4 +42,15 @@ describe('NOCK_OFF env var', () => {
expect(body).to.equal(responseBody)
scope.done()
})

it('when true before import, Nock does not activate', async () => {
nock.restore()
const originalClient = http.ClientRequest

delete require.cache[require.resolve('..')]
const newNock = require('..')

expect(http.ClientRequest).to.equal(originalClient)
expect(newNock.isActive()).to.equal(false)
})
})