How to use the enableNetConnect function from nock

Find comprehensive JavaScript nock.enableNetConnect code examples handpicked from public code repositorys.

nock.enableNetConnect is a function in the Nock library that allows outgoing network connections to be made for certain domains or URLs, while still intercepting all other network traffic.

4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
    nock.disableNetConnect();
});

afterEach(() => {
    nock.cleanAll();
    nock.enableNetConnect();
});

it('returns 200 for a step that exists', () => {
    nock('https://store.screwdriver.cd')
fork icon161
star icon969
watch icon65

34
35
36
37
38
39
40
41
42
43
44
}


describe('action', () => {
  beforeAll(() => {
    nock.disableNetConnect();
    nock.enableNetConnect(
      /(https:\/\/ccc-api.retailsvc.com|https:\/\/identitytoolkit.googleapis.com)/,
    );
  });

fork icon10
star icon28
watch icon37

+ 15 other calls in file

How does nock.enableNetConnect work?

nock.enableNetConnect is a function in the Nock library that allows outgoing network connections to be made for certain domains or URLs, while still intercepting all other network traffic. By default, when Nock intercepts a network request, it prevents the request from actually being sent and responds with a mocked response instead. However, in some cases, you may want to allow certain requests to go through to the real network, while still intercepting others. nock.enableNetConnect provides a way to do this by whitelisting certain domains or URLs for outgoing network connections. When nock.enableNetConnect is called with a whitelist, Nock intercepts all network requests by default but allows outgoing network connections to the whitelisted domains or URLs. This can be useful in situations where you need to make real network requests to a third-party service or API, but you still want to use Nock to mock responses for other requests. When you no longer need to allow outgoing network connections, you can call nock.disableNetConnect() to restore Nock's default behavior of intercepting all network traffic. Overall, nock.enableNetConnect provides a flexible way to work with real network requests and mocked responses using the Nock library.

48
49
50
51
52
53
54
55
56
57
    });
}

// Allow localhost
// Multiple enableNetConnect with different hosts overwrite each other, so we need to add one and use the allowedNetworkDomains variable
nock.enableNetConnect((host) => {
    if (host.includes('127.0.0.1')) {
        return true;
    }
    for (const h of allowedNetworkDomains) {
fork icon0
star icon0
watch icon1

+ 9 other calls in file

7
8
9
10
11
12
13
14
15
16
module.exports = tnock
function tnock (t, host, opts) {
  nock.disableNetConnect()
  const server = nock(host, opts)
  t.teardown(function () {
    nock.enableNetConnect()
    server.done()
  })
  return server
}
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const nock = require("nock");

// Allow outgoing network connections to example.com and api.example.com
nock.enableNetConnect(["example.com", "api.example.com"]);

// Define a mocked response for a request to example.com
nock("https://example.com")
  .get("/test")
  .reply(200, { message: "Hello from example.com!" });

// Make a real request to api.example.com
https.get("https://api.example.com/test", (res) => {
  res.on("data", (chunk) => {
    console.log(chunk.toString());
  });
});

// Make a mocked request to example.com
https.get("https://example.com/test", (res) => {
  res.on("data", (chunk) => {
    console.log(chunk.toString());
  });
});

// Disable outgoing network connections
nock.disableNetConnect();

In this example, we use nock.enableNetConnect to allow outgoing network connections to example.com and api.example.com, while still intercepting all other network traffic. We then define a mocked response for a request to example.com, but make a real request to api.example.com. Because api.example.com is whitelisted for outgoing network connections, the real request is allowed to go through, while the request to example.com is intercepted and handled by the mock response. After making the requests, we disable outgoing network connections using nock.disableNetConnect(), which restores Nock's default behavior of intercepting all network traffic.

84
85
86
87
88
89
90
91
92
93
94
95
  })
}


beforeAll(() => {
  nock.disableNetConnect()
  nock.enableNetConnect(/localhost|(products|pandas):4000/)


  // Since we use an Apollo Studio Key to enable federation, we need to mock the usage reporting
  // This is not critical, as we already have a nock.disableNetConnect() nothing is sent to the internet
  // But removes noisy logs
fork icon0
star icon0
watch icon1