Skip to content

Commit

Permalink
Change scope.remove to also remove interceptor from scope.interceptors
Browse files Browse the repository at this point in the history
Note: for consistency between scope.interceptors and scope.keyedInterceptors,
interceptors are now only pushed to scope.interceptors when they are added,
not when they are created. That guarantees both collections are consistent.
  • Loading branch information
mbargiel committed Feb 17, 2024
1 parent 48875f7 commit 1e57041
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/scope.js
Expand Up @@ -107,6 +107,7 @@ class Scope extends EventEmitter {
this.keyedInterceptors[key] = []
}
this.keyedInterceptors[key].push(interceptor)
this.interceptors.push(interceptor)
addInterceptor(
this.basePath,
interceptor,
Expand All @@ -123,6 +124,7 @@ class Scope extends EventEmitter {
if (arr.length === 0) {
delete this.keyedInterceptors[key]
}
this.interceptors.splice(this.interceptors.indexOf(interceptor), 1)
}
}

Expand All @@ -135,7 +137,6 @@ class Scope extends EventEmitter {
interceptorOptions,
)

this.interceptors.push(ic)
return ic
}

Expand Down
28 changes: 28 additions & 0 deletions tests/got/test_remove_interceptor.js
Expand Up @@ -53,6 +53,34 @@ describe('`removeInterceptor()`', () => {
expect(scope.pendingMocks()).to.deep.equal([])
})

it('reflects the removal in `interceptors` of its scope', () => {
const givenInterceptor = nock('http://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')

expect(scope.interceptors).to.deep.equal([
givenInterceptor,
])

expect(nock.removeInterceptor(givenInterceptor)).to.be.true()

expect(scope.interceptors).to.deep.equal([])
})

it('reflects the removal in `keyedInterceptors` of its scope', () => {
const givenInterceptor = nock('http://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')

expect(scope.keyedInterceptors).to.deep.equal({
'GET http://example.test:80/somepath': [
givenInterceptor,
]
})

expect(nock.removeInterceptor(givenInterceptor)).to.be.true()

expect(scope.keyedInterceptors).to.deep.equal({})
})

it('removes given interceptor for https', async () => {
const givenInterceptor = nock('https://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')
Expand Down
23 changes: 22 additions & 1 deletion tests/got/test_scope.js
Expand Up @@ -87,18 +87,39 @@ describe('`Scope#remove()`', () => {
expect(scope.activeMocks()).to.deep.equal([])
})

it('removes the interceptor from its collections', () => {
const scope = nock('http://example.test').get('/').reply(200)
const key = 'GET http://example.test:80/'
const interceptor = scope.interceptors[0]

// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
expect(scope.keyedInterceptors).to.deep.equal({ [key]: [interceptor] })

// Act.
scope.remove(key, scope.interceptors[0])

// Assert.
expect(scope.interceptors).to.deep.equal([])
expect(scope.keyedInterceptors).to.deep.equal({})
})

it('when the key is nonexistent, does nothing', () => {
const scope = nock('http://example.test').get('/').reply(200)
const key = 'GET http://example.test:80/'
const interceptor = scope.interceptors[0]

// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
expect(scope.keyedInterceptors).to.deep.equal({ [key]: [interceptor] })

// Act.
scope.remove('GET http://bogus.test:80/', scope.interceptors[0])
scope.remove('GET http://bogus.test:80/', interceptor)

// Assert.
expect(scope.activeMocks()).to.deep.equal([key])
expect(scope.interceptors).to.deep.equal([interceptor])
expect(scope.keyedInterceptors).to.deep.equal({ [key]: [interceptor] })
})
})

Expand Down

0 comments on commit 1e57041

Please sign in to comment.