Skip to content

Commit 8aab603

Browse files
author
Michael Solomon
authored
Push persisted interceptors to the end instead of ignore on remove (#2350)
Currently, if you do: ``` nock(..).get('/').reply(200).persist(); nock(..).get('/').reply(400); // this will never get called ``` The second interceptor won't get called ever. But sometimes in tests (queue for examples), I want to do: ``` beforeAll(() => { // some default behaviour nock(..).get('/').reply(200).persist(); }); test('should ...', () => { // test specific scenario for this same endpoint above nock(..).get('/').reply(400) }) ``` I find a workaround using the body matcher function: ``` const createInterceptor = () => { nock(..).get('/', () => { createInterceptor(); return true; }) .reply(200) } ``` Which mimics the wanted behavior but is slower and way less elegant.
1 parent 8a38f41 commit 8aab603

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ const scope = nock('http://example.com')
10021002
.reply(200, 'Persisting all the way')
10031003
```
10041004

1005-
Note that while a persisted scope will always intercept the requests, it is considered "done" after the first interception.
1005+
Note that while a persisted scope will always intercept the requests, it is considered "done" after the first interception, and they are pushed to the bottom of the stack after consumption.
10061006

10071007
If you want to stop persisting an individual persisted mock you can call `persist(false)`:
10081008

lib/intercept.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function addInterceptor(key, interceptor, scope, scopeOptions, host) {
110110
}
111111

112112
function remove(interceptor) {
113-
if (interceptor.__nock_scope.shouldPersist() || --interceptor.counter > 0) {
113+
if (--interceptor.counter > 0) {
114114
return
115115
}
116116

@@ -122,6 +122,12 @@ function remove(interceptor) {
122122
// matching instance. I'm also not sure why we couldn't delete _all_
123123
// matching instances.
124124
interceptors.some(function (thisInterceptor, i) {
125+
if (interceptor.__nock_scope.shouldPersist()) {
126+
return thisInterceptor === interceptor
127+
? interceptors.push(interceptors.splice(i, 1)[0])
128+
: false
129+
}
130+
125131
return thisInterceptor === interceptor ? interceptors.splice(i, 1) : false
126132
})
127133
}

tests/test_persist_optionally.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ describe('`persist()`', () => {
144144
expect(scope.isDone()).to.be.true()
145145
})
146146

147+
it('persisted mocks go to the bottom of the stack after matching', async () => {
148+
const scope = nock('http://example.test')
149+
.persist()
150+
.get('/')
151+
.reply(200, 'Persisting all the way')
152+
153+
const specificTestScope = nock('http://example.test')
154+
.get('/')
155+
.reply(201, 'return different response once')
156+
157+
await got('http://example.test/')
158+
159+
expect(scope.isDone()).to.be.true()
160+
161+
await got('http://example.test/')
162+
163+
expect(specificTestScope.isDone()).to.be.true()
164+
})
165+
147166
it('persisted mocks appear in `pendingMocks()`', async () => {
148167
const scope = nock('http://example.test')
149168
.get('/abc')

0 commit comments

Comments
 (0)