diff --git a/README.md b/README.md index 2a652c5c7..715500f98 100644 --- a/README.md +++ b/README.md @@ -1315,6 +1315,23 @@ const interceptor = nock('http://example.org').get('somePath') nock.removeInterceptor(interceptor) ``` +### .removeInterceptorByPredictor(predictor) + +The purpose is the same as removeInterceptor but here you can use predictor(a function) to determinate which interceptor instance to be removed, it gives way more expansibility to removal logics. + +Examples: + +```js +const removed = nock.removeInterceptorByPredictor( + ({ interceptor, basePath }) => { + return (interceptor.path === 'abc' && basePath = 'http://localhost') + } +) + +// here you can check if interceptor is acutally removed. +console.log(removed && removed[0].path) // should be 'abc' in console. +``` + ## Events A scope emits the following events: diff --git a/index.js b/index.js index 1c8d00d5a..c27d30819 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ const { pendingMocks, activeMocks, removeInterceptor, + removeInterceptorByPredictor, disableNetConnect, enableNetConnect, removeAll, @@ -27,6 +28,7 @@ Object.assign(module.exports, { pendingMocks, activeMocks, removeInterceptor, + removeInterceptorByPredictor, disableNetConnect, enableNetConnect, cleanAll: removeAll, diff --git a/lib/intercept.js b/lib/intercept.js index a95eb514c..2ae4e2fab 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -195,6 +195,25 @@ function interceptorsFor(options) { return undefined } +function removeInterceptorByPredictor(predictor) { + const removedInterceptors = [] + for (const [baseUrl, { key, interceptors }] of Object.entries( + allInterceptors + )) { + for (let i = 0; i < interceptors.length; i++) { + const interceptor = interceptors[i] + if (predictor({ baseUrl, interceptor })) { + interceptors.splice(i, 1) + interceptor.scope.remove(key, interceptor) + // reserve removed interceptors for better observation against the removal execution + removedInterceptors.push({ ...interceptor }) + } + } + } + + return removedInterceptors.length ? removedInterceptors : undefined +} + function removeInterceptor(options) { // Lazily import to avoid circular imports. const Interceptor = require('./interceptor') @@ -438,6 +457,7 @@ module.exports = { remove, removeAll, removeInterceptor, + removeInterceptorByPredictor, isOn, activate, isActive, diff --git a/tests/test_remove_interceptor.js b/tests/test_remove_interceptor.js index 2e6c0b152..65b2798b1 100644 --- a/tests/test_remove_interceptor.js +++ b/tests/test_remove_interceptor.js @@ -6,6 +6,46 @@ const got = require('./got_client') require('./setup') +describe('removeInterceptorByPredictor', () => { + context('when invoked with an Interceptor instance', () => { + it('should remove interceptor matched by given predictor', async () => { + const givenInterceptor = nock('http://example.test').get('/somepath') + givenInterceptor.reply(200, 'hey') + + const predictor = ({ interceptor }) => interceptor.path === '/somepath' + + const [removed] = nock.removeInterceptorByPredictor(predictor) + + expect(removed.path).to.be.equals('/somepath') + + nock('http://example.test').get('/somepath').reply(202, 'other-content') + + const { statusCode, body } = await got('http://example.test/somepath') + + expect(statusCode).to.equal(202) + expect(body).to.equal('other-content') + }) + + it('should not remove interceptor if given predictor matches nothing', async () => { + const givenInterceptor = nock('http://example.test').get('/somepath') + givenInterceptor.reply(200, 'hey') + + const predictor = () => false + + const removed = nock.removeInterceptorByPredictor(predictor) + + expect(removed).to.be.undefined() + + nock('http://example.test').get('/somepath').reply(202, 'other-content') + + const { statusCode, body } = await got('http://example.test/somepath') + + expect(statusCode).to.equal(200) + expect(body).to.equal('hey') + }) + }) +}) + describe('`removeInterceptor()`', () => { context('when invoked with an Interceptor instance', () => { it('remove interceptor removes given interceptor', async () => { diff --git a/types/index.d.ts b/types/index.d.ts index 9a81a905a..b5956cdf4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -20,6 +20,12 @@ declare namespace nock { function pendingMocks(): string[] function activeMocks(): string[] function removeInterceptor(interceptor: Interceptor | ReqOptions): boolean + function removeInterceptorByPredictor( + predictor: (arg: { + interceptor: Interceptor & ReqOptions + basePath: string + }) => void + ): Interceptor[] | undefined function disableNetConnect(): void function enableNetConnect( matcher?: string | RegExp | ((host: string) => boolean) diff --git a/types/tests.ts b/types/tests.ts index cfc870137..cf75d31a7 100644 --- a/types/tests.ts +++ b/types/tests.ts @@ -735,6 +735,9 @@ nock.removeInterceptor({ const interceptor = nock('http://example.test').get('somePath') nock.removeInterceptor(interceptor) +// .removeInterceptorByPredictor() +nock.removeInterceptorByPredictor(({ interceptor }) => interceptor.path === '') + // Events /// Global no match event nock.emitter.on('no match', (req: any) => {})