Skip to content

Quadratic number of interceptors reported by activeMocks #2352

@mbargiel

Description

@mbargiel

Please avoid duplicates

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: #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?

  • yes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions