Skip to content

Commit

Permalink
Push persisted interceptors to the end instead of ignore on remove (n…
Browse files Browse the repository at this point in the history
…ock#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.
  • Loading branch information
mikicho committed Jun 25, 2023
1 parent 8a38f41 commit 8aab603
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ const scope = nock('https://example.com')
.reply(200, 'Persisting all the way')
```

Note that while a persisted scope will always intercept the requests, it is considered "done" after the first interception.
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.

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

Expand Down
8 changes: 7 additions & 1 deletion lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function addInterceptor(key, interceptor, scope, scopeOptions, host) {
}

function remove(interceptor) {
if (interceptor.__nock_scope.shouldPersist() || --interceptor.counter > 0) {
if (--interceptor.counter > 0) {
return
}

Expand All @@ -122,6 +122,12 @@ function remove(interceptor) {
// matching instance. I'm also not sure why we couldn't delete _all_
// matching instances.
interceptors.some(function (thisInterceptor, i) {
if (interceptor.__nock_scope.shouldPersist()) {
return thisInterceptor === interceptor
? interceptors.push(interceptors.splice(i, 1)[0])
: false
}

return thisInterceptor === interceptor ? interceptors.splice(i, 1) : false
})
}
Expand Down
19 changes: 19 additions & 0 deletions tests/test_persist_optionally.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ describe('`persist()`', () => {
expect(scope.isDone()).to.be.true()
})

it('persisted mocks go to the bottom of the stack after matching', async () => {
const scope = nock('https://example.test')
.persist()
.get('/')
.reply(200, 'Persisting all the way')

const specificTestScope = nock('https://example.test')
.get('/')
.reply(201, 'return different response once')

await got('https://example.test/')

expect(scope.isDone()).to.be.true()

await got('https://example.test/')

expect(specificTestScope.isDone()).to.be.true()
})

it('persisted mocks appear in `pendingMocks()`', async () => {
const scope = nock('https://example.test')
.get('/abc')
Expand Down

0 comments on commit 8aab603

Please sign in to comment.