Skip to content

Commit

Permalink
fix: nock.removeInterceptor can remove the wrong Intercept (#2497)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikicho committed Jul 13, 2023
1 parent bd4783b commit 92de0de
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ function removeInterceptor(options) {
) {
for (let i = 0; i < allInterceptors[baseUrl].interceptors.length; i++) {
const interceptor = allInterceptors[baseUrl].interceptors[i]
if (interceptor._key === key) {
if (
options instanceof Interceptor
? interceptor === options
: interceptor._key === key
) {
allInterceptors[baseUrl].interceptors.splice(i, 1)
interceptor.scope.remove(key, interceptor)
break
Expand Down
7 changes: 3 additions & 4 deletions tests/test_remove_interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ const got = require('./got_client')
describe('`removeInterceptor()`', () => {
context('when invoked with an Interceptor instance', () => {
it('remove interceptor removes given interceptor', async () => {
const newScope = nock('http://example.test')
.get('/somepath')
.reply(202, 'other-content')
const givenInterceptor = nock('http://example.test').get('/somepath')
givenInterceptor.reply(200, 'hey')

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

const newScope = nock('http://example.test')
.get('/somepath')
.reply(202, 'other-content')

const { statusCode, body } = await got('http://example.test/somepath')

expect(statusCode).to.equal(202)
Expand Down

0 comments on commit 92de0de

Please sign in to comment.