You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you have multiple intercepts with method: 'POST' and the same basePath and _key but different values for _requestBody.
If you call removeInterceptor with a specific interceptor instance.
Then the first interceptor whose _key matches that of the interceptor instance will be removed.
The interceptor that is removed may or may not have the same _requestBody.
This depends on the order of the interceptors.
Possible Improvement
Have a unique ID field on the Interceptor class and using that to compare interceptors within the removeInterceptor method.
or
Include interceptor._requestBody in the comparison of interceptors within the removeInterceptor method.
Alternatives
It's currently possible to mitigate this issue by setting your 'POST' interceptors for a specific endpoint in the order that you will need to remove them in.
e.g. If you have interceptors A, B and C and in your tests you need to be able to remove interceptor B without removing the other interceptors:
A = nock('http://www.example.com').post('/', requestBodyA);
B = nock('http://www.example.com').post('/', requestBodyB);
C = nock('http://www.example.com').post('/', requestBodyC);
trying to remove interceptor B:
nock.removeInterceptor(B);
Will actually remove interceptor A.
By changing the order you create the interceptors
B = nock('http://www.example.com').post('/', requestBodyB);
A = nock('http://www.example.com').post('/', requestBodyA);
C = nock('http://www.example.com').post('/', requestBodyC);
trying to remove interceptor B:
nock.removeInterceptor(B);
Will then remove the correct interceptor.
Has the feature been requested before?
Not that I can see
If the feature request is accepted, would you be willing to submit a PR?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.
Context
When you have multiple intercepts with method: 'POST' and the same basePath and _key but different values for _requestBody.
If you call removeInterceptor with a specific interceptor instance.
Then the first interceptor whose _key matches that of the interceptor instance will be removed.
The interceptor that is removed may or may not have the same _requestBody.
This depends on the order of the interceptors.
Possible Improvement
Have a unique ID field on the Interceptor class and using that to compare interceptors within the removeInterceptor method.
or
Include interceptor._requestBody in the comparison of interceptors within the removeInterceptor method.
Alternatives
It's currently possible to mitigate this issue by setting your 'POST' interceptors for a specific endpoint in the order that you will need to remove them in.
e.g. If you have interceptors A, B and C and in your tests you need to be able to remove interceptor B without removing the other interceptors:
trying to remove interceptor B:
nock.removeInterceptor(B);
Will actually remove interceptor A.
By changing the order you create the interceptors
trying to remove interceptor B:
nock.removeInterceptor(B);
Will then remove the correct interceptor.
Has the feature been requested before?
Not that I can see
If the feature request is accepted, would you be willing to submit a PR?
Yes
one possible change in lib/intercept.js:
The text was updated successfully, but these errors were encountered: