Skip to content

Commit

Permalink
feat: add removeInterceptorByPredictor to make interceptor removal lo…
Browse files Browse the repository at this point in the history
…gics customizable
  • Loading branch information
Frank Wu committed Feb 9, 2021
1 parent 0f7b52e commit 162c30f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -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:
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
40 changes: 40 additions & 0 deletions tests/test_remove_interceptor.js
Expand Up @@ -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 () => {
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

0 comments on commit 162c30f

Please sign in to comment.