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

Simple interceptor enable option #2256

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Simple interceptor enable option #2256

wants to merge 5 commits into from

Conversation

nopeless
Copy link

This PR allows a simple enabled property to be processed

Before:

  • In order to temporary remove an interceptor, one must call .removeInterceptor and call .get on the interceptor with the same function call again

After:

  • Just set enabled property to false

What this PR covers

  • Code changes
  • Unit tests (2 more)
  • Documentation update

Summary of code change:

  • Created new property called enabled in Interceptor
  • Added an additional filter condition in InterceptedRequestRouter#startPlayback for matchedInterceptor list

@gr2m
Copy link
Member

gr2m commented Nov 30, 2021

Can you please create an issue to discuss first? We are trying to clean up the current code base and APIs of nock, this will make it more complex. It feels like a patch for a bigger underlying problem. I don't think that it's something we can address right now

@nopeless
Copy link
Author

nopeless commented Dec 1, 2021

@gr2m I think it is self-evident that there is no simple way to toggle an interceptor. If there is a cleanup of the current code base, it should have a way to toggle an interceptor.
I'm not sure why no one else wanted this functionality in the past, but I don't see why this can't be a temporary functionality until the rewrite.
At least I need to use this functionality, and there isn't a way to achieve this without creating huge code inefficiencies.

@gr2m
Copy link
Member

gr2m commented Dec 1, 2021

We can leave the PR open to see if others run into the same problem. nock has been around for a long, long time with the current APIs. I cannot recall anyone asking for this feature in particular.

@nopeless
Copy link
Author

nopeless commented Dec 2, 2021

@gr2m Sure, I'm fine with that 👍

@nopeless
Copy link
Author

nopeless commented Jan 4, 2022

let me know if you are interested in this kind of feature soon

@ShaharAdskAcc
Copy link

@gr2m , @nopeless
This is something I looked for myself,

My use case is:
in 95% percent of the time, I want to use the same nock scope,
but for 5% percentage of the tests, I need to disable those scopes temporarily for those test because I want to change the response to something different.

@ShaharAdskAcc
Copy link

ShaharAdskAcc commented Jun 8, 2023

Until it will be merge (if it ever will) - here is a current work around

export function nockDisableScope(scope: nock.Scope) {
  // @ts-ignore
  const interceptors = scope.keyedInterceptors;
  // @ts-ignore
  scope.interceptors.forEach(nock.removeInterceptor);
  return interceptors;
}

export function nockDisableScopes(scopes: nock.Scope[]): [nock.Scope, any[]][] {
  const scopesWithListeners: [nock.Scope, any[]][] = [];
  for (const scope of scopes) {
    scopesWithListeners.push([scope, nockDisableScope(scope)]);
  }
  return scopesWithListeners;
}

export function nockEnableScope(scope: nock.Scope, interceptors: any[]) {
  for (const [key, keyInterceptors] of Object.entries(interceptors)) {
    // @ts-ignore
    const previousInterceptors = [...keyInterceptors];
    for (const currentKeyInterceptor of previousInterceptors) {
      // @ts-ignore
      scope.add(key, currentKeyInterceptor);
    }
  }
}

export function nockEnableScopes(scopesWithListeners: [nock.Scope, any[]][]): void {
  for (const [scope, scopeEventWithListeners] of scopesWithListeners) {
    nockEnableScope(scope, scopeEventWithListeners);
  }
}
  • Edit fixed bug in nockEnableScope.

@nopeless
Copy link
Author

nopeless commented Jun 8, 2023

Until it will be merge (if it ever will) - here is a current work around

export function nockDisableScope(scope: nock.Scope) {
  // @ts-ignore
  const interceptors = scope.keyedInterceptors;
  // @ts-ignore
  scope.interceptors.forEach(nock.removeInterceptor);
  return interceptors;
}

export function nockDisableScopes(scopes: nock.Scope[]): [nock.Scope, any[]][] {
  const scopesWithListeners: [nock.Scope, any[]][] = [];
  for (const scope of scopes) {
    scopesWithListeners.push([scope, nockDisableScope(scope)]);
  }
  return scopesWithListeners;
}

export function nockEnableScope(scope: nock.Scope, interceptors: any[]) {
  for (const [key, keyInterceptors] of Object.entries(interceptors)) {
    // @ts-ignore
    const previousInterceptors = [...keyInterceptors];
    for (const currentKeyInterceptor of previousInterceptors) {
      // @ts-ignore
      scope.interceptors.push(keyInterceptors);
      // @ts-ignore
      if (!scope.keyedInterceptors[key]) {
        // @ts-ignore
        scope.keyedInterceptors[key] = [];
      }
      // @ts-ignore
      scope.keyedInterceptors[key]!.push(currentKeyInterceptor);
    }
  }
}

export function nockEnableScopes(scopesWithListeners: [nock.Scope, any[]][]): void {
  for (const [scope, scopeEventWithListeners] of scopesWithListeners) {
    nockEnableScope(scope, scopeEventWithListeners);
  }
}

Nice, I... don't use nock atm but ill get to this as well when I need it

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

Successfully merging this pull request may close these issues.

None yet

3 participants