Skip to content

enableNetConnect() no longer works with Regexp-like objects #1861

@nikaspran

Description

@nikaspran

This is not necessarily a bug, but something I would consider an undocumented feature. Before https://github.com/nock/nock/pull/1754/files#diff-503754c07b5098227646f913eee85885R58 it was possible to use enableNetConnect with a function, which greatly improved the flexibility of this mechanism.

As an example use case, we used to have a constantly changing Set of allowed hostnames which tests could add to. This is still possible to achieve by constructing a RegExp but it is much more awkward. There may be other use cases which cannot be converted to a RegExp at all.

What is the expected behavior?

This used to work:

const allowedOutsideHosts = [/* some hosts */];
nock.enableNetConnect({
  test: host => allowedOutsideHosts.includes(host), // or any function
});

What is the actual behavior?

You can no longer use "RegExp-like" objects (with a test function) to filter hosts for enableNetConnect(). It now defaults to a wildcard match matching anything.

Possible solutions

  1. Revert back to checking for a test function?
  2. Allow passing an explicit object with a test function

How to reproduce the issue

Runkit: 11.7.0
Runkit: 11.7.2

Versions

Software Version(s)
Nock 11.7.1
Node Any

I would be willing to submit a PR for this over the next few days if you agree that it should be done. I personally think that option 2 might be the best as it would retain the proper typechecks.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions