How to use the activate function from nock

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

nock.activate is a function in the Nock library for Node.js that activates HTTP request interception and mocking for a given scope.

2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
let inputFiles, inputFileStreams, outputContent, outputHeaders;

// before the tests, disable network connections to ensure tests never hit real network
before(() => {
  // if another test suite de-activated nock, we need to re-activate it
  if (!nock.isActive()) nock.activate();
  nock.disableNetConnect();
});

// after the tests, re-enable network connections
fork icon14
star icon34
watch icon9

+ 19 other calls in file

118
119
120
121
122
123
124
125
126
127
  if (this.#stack.length) {
    this[_saveState](this.#stack[this.#stack.length - 1])
  }

  nock.restore()
  nock.activate()
  this.#state = PAUSED
}

resume () {
fork icon3
star icon5
watch icon7

+ 3 other calls in file

How does nock.activate work?

The nock.activate function in the Nock library for Node.js activates HTTP request interception and mocking for a given scope. When called, nock.activate intercepts all HTTP requests that match the specified scope and returns a mocked response instead of sending the request to the network. This can be useful for testing and development, as it allows you to simulate different HTTP responses without actually making network requests. The nock library allows you to create "scopes" that define the URLs and HTTP methods that should be intercepted and mocked. Once a scope has been defined, it can be activated using nock.activate. When activated, nock overrides the http.request and https.request functions in Node.js to intercept all HTTP requests made by the program. If a request matches the specified scope, nock returns a mocked response with the specified status code, headers, and body. It's important to note that nock.activate should be used with caution, as it can interfere with the normal operation of your program and cause unexpected behavior. It is typically used in testing and development environments to isolate and test specific parts of a program that rely on HTTP requests. In summary, nock.activate is a function in the Nock library for Node.js that activates HTTP request interception and mocking for a specified scope, and should be used with caution to avoid interfering with the normal operation of your program.

11
12
13
14
15
16
17
18
19
20
delete process.env.PROXY_PORT;
delete process.env.PROXY_AUTH;
delete process.env.PROXY_URL_REGEX;

nock.restore();
nock.activate();
check = () => {};

const http = require('http');
const httpWrap = (func) => {
fork icon0
star icon1
watch icon1

+ 4 other calls in file

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
27
28
29
30
31
32
33
const assert = require("assert");
const http = require("http");
const nock = require("nock");

// Define a mock API endpoint
const scope = nock("http://localhost:8080")
  .get("/api/users")
  .reply(200, { users: [{ name: "Alice" }, { name: "Bob" }] });

// Activate HTTP request interception
nock.activate();

// Make a request to the mock API endpoint
http.get("http://localhost:8080/api/users", function (res) {
  let data = "";

  // Collect the response data
  res.on("data", function (chunk) {
    data += chunk;
  });

  // Assert that the response contains the expected data
  res.on("end", function () {
    assert.equal(
      data,
      JSON.stringify({ users: [{ name: "Alice" }, { name: "Bob" }] })
    );
    console.log("Test passed!");
  });
});

// Deactivate HTTP request interception
nock.restore();

In this example, we first define a mock API endpoint using nock, which intercepts all HTTP GET requests to /api/users on http://localhost:8080 and returns a response with a status code of 200 and a JSON payload containing a list of users. We then activate HTTP request interception using nock.activate(), which overrides the http.request and https.request functions in Node.js to intercept all HTTP requests made by the program. We make a request to the mock API endpoint using http.get, and collect the response data using the data event. We then assert that the response data contains the expected payload using assert.equal. Finally, we deactivate HTTP request interception using nock.restore(), which restores the original behavior of the http.request and https.request functions in Node.js.