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: make interceptors work in any order even when filteringScope #1819

Merged
merged 3 commits into from Jan 3, 2020

Conversation

salmanm
Copy link
Contributor

@salmanm salmanm commented Nov 20, 2019

Fixed #1818

tests/test_intercept.js Outdated Show resolved Hide resolved
@salmanm
Copy link
Contributor Author

salmanm commented Nov 26, 2019

@paulmelnikow Wanna review this PR? Or invite someone who would?

@mastermatt
Copy link
Member

@salmanm thanks for taking this on.

The interceptorsFor function is one I'd really like to get rid of. I've been working on it in other refactors, but I have no timeline for that so we should move forward with this change.

As noted in the original issue, the bug is that the function returns early before it has a chance to set __nock_filteredScope on all the Interceptors.
Your change fixes that, but it means the filteringScope function has to be run for each Interceptor in the array. I always like to assume that user input function can be expensive.

My suggestion is to replace lines 153-169 with this:

    const { filteringScope } = interceptors[0].scope.scopeOptions

    // If scope filtering function is defined and returns a truthy value then
    // we have to treat this as a match.
    if (filteringScope && filteringScope(basePath)) {
      debug('found matching scope interceptor')

      // Keep the filtered scope (its key) to signal the rest of the module
      // that this wasn't an exact but filtered match.
      interceptors.forEach(ic => {
        ic.__nock_filteredScope = key
      })
      return interceptors
    }

Things to note: interceptors is guaranteed to always have at least one item, and all the interceptors in the array are guaranteed to have the same scope.
.__nock_scopeOptions is a ref to .scope.scopeOptions and .__nock_scopeKey is a copy of key.
I think we should move away from these magic dunder attributes.

@salmanm
Copy link
Contributor Author

salmanm commented Nov 27, 2019

Ok those two clarifications changes things. I didn't know that they're guaranteed to have the same scope. I'll change it to you suggestion of a sub loop to set the __nock_filteredScope.

and yes, I agree we should get rid of those magic attribs, but perhaps that's a bigger refactor and can be done separate from this PR?

@salmanm
Copy link
Contributor Author

salmanm commented Nov 28, 2019

Aye pushed the suggested changes. 👍

@salmanm
Copy link
Contributor Author

salmanm commented Dec 2, 2019

@mastermatt Would you like to take another look?

Copy link
Member

@mastermatt mastermatt left a comment

Choose a reason for hiding this comment

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

Works for me.

@paulmelnikow ?

@salmanm
Copy link
Contributor Author

salmanm commented Dec 4, 2019

Can we move this forward please? Or let me know if there are any changes. Thanks @paulmelnikow.

@salmanm salmanm reopened this Dec 10, 2019
@paulmelnikow paulmelnikow merged commit bd39dff into nock:master Jan 3, 2020
@nockbot
Copy link
Collaborator

nockbot commented Jan 3, 2020

🎉 This PR is included in version 11.7.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

kennethaasan pushed a commit to kennethaasan/nock that referenced this pull request Jan 10, 2020
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.

Interceptors don't match when used with different order "when using filteringScope option"
5 participants