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

add removeInterceptorByPredictor to make interceptor removal logics customizable #2141

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -1333,6 +1333,22 @@ nock.removeInterceptor(interceptor)
const interceptor = nock('http://example.org').get('somePath')
interceptor.reply(200, 'OK')
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
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -10,6 +10,7 @@ const {
pendingMocks,
activeMocks,
removeInterceptor,
removeInterceptorByPredictor,
disableNetConnect,
enableNetConnect,
removeAll,
Expand All @@ -27,6 +28,7 @@ Object.assign(module.exports, {
pendingMocks,
activeMocks,
removeInterceptor,
removeInterceptorByPredictor,
disableNetConnect,
enableNetConnect,
cleanAll: removeAll,
Expand Down
20 changes: 20 additions & 0 deletions lib/intercept.js
Expand Up @@ -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')
Expand Down Expand Up @@ -438,6 +457,7 @@ module.exports = {
remove,
removeAll,
removeInterceptor,
removeInterceptorByPredictor,
isOn,
activate,
isActive,
Expand Down
42 changes: 42 additions & 0 deletions tests/test_remove_interceptor.js
Expand Up @@ -4,6 +4,48 @@ const { expect } = require('chai')
const nock = require('..')
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 () => {
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions types/tests.ts
Expand Up @@ -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) => {})
Expand Down