Repository: nock/nock (js)

Issue:
Quadratic number of interceptors reported by activeMocks
### Please avoid duplicates

- [X] I checked [all open bugs](https://github.com/nock/nock.js/issues?q=is%3Aissue+is%3Aopen+label%3Abug) and none of them matched my problem.

### Reproducible test case

See example snippet in description.

### Nock Version

13.2.4

### Node Version

16.13.1

### TypeScript Version

_No response_

### What happened?

When registering multiple interceptors in the same scope, calling `nock.activeMocks()` reports too many active mocks: all those interceptors are present `n^2` times. For instance, registering 5 interceptors in a scope will result in 25 mocks reported by `nock.activeMocks()`.

```
const nock = require('nock')
const scope = nock('http://example.com')
  .get('/').reply(200)
  .get('/other').reply(200)
  .get('/third').reply(201)
console.log(nock.activeMocks())
```
Expected logged:
```
[
  'GET http://example.com:80/',
  'GET http://example.com:80/other',
  'GET http://example.com:80/third',
]
```
Actual:
```
[
  'GET http://example.com:80/',
  'GET http://example.com:80/other',
  'GET http://example.com:80/third',
  'GET http://example.com:80/',
  'GET http://example.com:80/other',
  'GET http://example.com:80/third',
  'GET http://example.com:80/',
  'GET http://example.com:80/other',
  'GET http://example.com:80/third'
]
```

Note: this appears to have been reported in the past, but the Issue was closed due to staleness: https://github.com/nock/nock/issues/600. That Issue mentioned the same problem for `nock.pendingMocks()`, which, I guess, has the same root cause.


### Would you be interested in contributing a fix?

- [x] yes

Pull request description:
fix: remove duplicates from `activeMocks()` and `pendingMocks()`
The `interceptorScopes()` function is used by `activeMocks()` and `pendingMocks()` to retrieve the list of all scopes. That list is built by concatenating the scopes of every interceptor. When multiple interceptors are registered on the same scope, the scope will be returned multiple times, causing `activeMocks()` and `pendingMocks()` to include duplicates.

This PR fixes the issue by ensuring there is no duplicate returned by `interceptorScopes()`.

Fixes #2352 (and #600, which also reported this issue)

Patch:
diff --git a/lib/intercept.js b/lib/intercept.js
index cf266d5..bcc1319 100644
--- a/lib/intercept.js
+++ b/lib/intercept.js
@@ -350,7 +350,8 @@ function interceptorScopes() {
   const nestedInterceptors = Object.values(allInterceptors).map(
     i => i.interceptors,
   )
-  return [].concat(...nestedInterceptors).map(i => i.scope)
+  const scopes = new Set([].concat(...nestedInterceptors).map(i => i.scope))
+  return [...scopes]
 }
 
 function isDone() {


Test patch:
diff --git a/tests/got/test_nock_lifecycle.js b/tests/got/test_nock_lifecycle.js
index 92a07ef..622f307 100644
--- a/tests/got/test_nock_lifecycle.js
+++ b/tests/got/test_nock_lifecycle.js
@@ -163,6 +163,22 @@ describe('Nock lifecycle functions', () => {
       await got('http://example.test/')
       expect(nock.activeMocks()).to.be.empty()
     })
+
+    it("activeMocks doesn't return duplicate mocks", () => {
+      nock('http://example.test')
+        .get('/')
+        .reply()
+        .get('/second')
+        .reply()
+        .get('/third')
+        .reply()
+
+      expect(nock.activeMocks()).to.deep.equal([
+        'GET http://example.test:80/',
+        'GET http://example.test:80/second',
+        'GET http://example.test:80/third',
+      ])
+    })
   })
 
   describe('resetting nock catastrophically while a request is in progress', () => {