From fcea7792954af0ec25d1d12e1f26ad4ca74e5375 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Wed, 16 Aug 2023 14:03:46 -0700 Subject: [PATCH] fix: revert #2350 (Push persisted interceptors to the end instead of ignore on remove) (#2511) This reverts commit 8aab603f2b884f49618fbfdcc3c9529cccdc8975. See #2500 --- README.md | 2 +- lib/intercept.js | 8 +------- tests/test_persist_optionally.js | 19 ------------------- 3 files changed, 2 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d465a5d9f..2f70d9f06 100644 --- a/README.md +++ b/README.md @@ -1005,7 +1005,7 @@ const scope = nock('http://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, and they are pushed to the bottom of the stack after consumption. +Note that while a persisted scope will always intercept the requests, it is considered "done" after the first interception. If you want to stop persisting an individual persisted mock you can call `persist(false)`: diff --git a/lib/intercept.js b/lib/intercept.js index a6220484a..d02ee4184 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -110,7 +110,7 @@ function addInterceptor(key, interceptor, scope, scopeOptions, host) { } function remove(interceptor) { - if (--interceptor.counter > 0) { + if (interceptor.__nock_scope.shouldPersist() || --interceptor.counter > 0) { return } @@ -122,12 +122,6 @@ 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 }) } diff --git a/tests/test_persist_optionally.js b/tests/test_persist_optionally.js index 1da7d6d21..6fe06d9b0 100644 --- a/tests/test_persist_optionally.js +++ b/tests/test_persist_optionally.js @@ -144,25 +144,6 @@ describe('`persist()`', () => { expect(scope.isDone()).to.be.true() }) - it('persisted mocks go to the bottom of the stack after matching', async () => { - const scope = nock('http://example.test') - .persist() - .get('/') - .reply(200, 'Persisting all the way') - - const specificTestScope = nock('http://example.test') - .get('/') - .reply(201, 'return different response once') - - await got('http://example.test/') - - expect(scope.isDone()).to.be.true() - - await got('http://example.test/') - - expect(specificTestScope.isDone()).to.be.true() - }) - it('persisted mocks appear in `pendingMocks()`', async () => { const scope = nock('http://example.test') .get('/abc')