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

fix: make nock.removeInterceptor ignore scope._persist and update scope.interceptors #2355

Open
wants to merge 2 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
6 changes: 2 additions & 4 deletions 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 @@ -117,15 +118,13 @@ class Scope extends EventEmitter {
}

remove(key, interceptor) {
if (this._persist) {
return
}
const arr = this.keyedInterceptors[key]
if (arr) {
arr.splice(arr.indexOf(interceptor), 1)
if (arr.length === 0) {
delete this.keyedInterceptors[key]
}
this.interceptors.splice(this.interceptors.indexOf(interceptor), 1)
}
}

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

this.interceptors.push(ic)
return ic
}

Expand Down
47 changes: 46 additions & 1 deletion tests/got/test_remove_interceptor.js
Expand Up @@ -6,7 +6,7 @@ const got = require('./got_client')

describe('`removeInterceptor()`', () => {
context('when invoked with an Interceptor instance', () => {
it('remove interceptor removes given interceptor', async () => {
it('removes given interceptor', async () => {
const newScope = nock('http://example.test')
.get('/somepath')
.reply(202, 'other-content')
Expand All @@ -23,6 +23,23 @@ describe('`removeInterceptor()`', () => {
newScope.done()
})

it('removes given interceptor even with `persist()`', 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').persist()

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

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

expect(statusCode).to.equal(202)
expect(body).to.equal('other-content')

newScope.done()
})

it('reflects the removal in `pendingMocks()`', () => {
const givenInterceptor = nock('http://example.test').get('/somepath')
const scope = givenInterceptor.reply(200, 'hey')
Expand All @@ -36,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
15 changes: 11 additions & 4 deletions tests/got/test_scope.js
Expand Up @@ -87,32 +87,39 @@ describe('`Scope#remove()`', () => {
expect(scope.activeMocks()).to.deep.equal([])
})

it('when a mock is persisted, does nothing', () => {
const scope = nock('http://example.test').persist().get('/').reply(200)
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.activeMocks()).to.deep.equal([key])
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