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

Conversation

mastermatt
Copy link
Member

While debugging the Jest memory leak, I was surprised to find the Nock
was "activating" twice when imported. This is because intercept.js
was calling activate() directly AND back was setting the mode, which
calls restore() and activate(). There is no need for this double
initialization. Think of the CPUs we could be saving.
It also makes debugging harder.
I also discovered that Nock "activates" even if the proc is started with
the NOCK_OFF env var set, which means Nock is polluting the global
scope even when callers have asked it not to.

Since it felt like the back.setMode() call was hiding in its module,
I've moved it to the entry index file and wrapped it in a isOn check.

While debugging the Jest memory leak, I was surprised to find the Nock
was "activating" twice when imported. This is because intercept.js
was calling `activate()` directly _AND_ back was setting the mode, which
calls `restore()` and `activate()`. There is no need for this double
initialization. Think of the CPUs we could be saving.
I also discovered that Nock "activates" even if the proc is started with
the `NOCK_OFF` env var set, which means Nock is polluting the global
scope even when callers have asked it not to.

Since it felt like the `back.setMode()` call was hiding in its module,
I've moved it to the entry index file and wrapped it in a `isOn` check.
// 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.


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

@mastermatt
Copy link
Member Author

@paulmelnikow I updated the comment. This should be good to go now.

@paulmelnikow
Copy link
Member

I'm not sure if this is a feature. I could see shipping it as a patch, or else as a breaking change along with the other changes being made in #1871.

@mastermatt mastermatt changed the title feature: only activate once, when on fix: only activate once, when on Feb 10, 2020
@mastermatt mastermatt merged commit a56a209 into nock:master Feb 10, 2020
@mastermatt mastermatt deleted the activate-once branch February 10, 2020 04:29
@nockbot
Copy link
Collaborator

nockbot commented Feb 10, 2020

🎉 This PR is included in version 11.8.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants