How to use the abortPendingRequests function from nock

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

nock.abortPendingRequests is a function in the Nock library that aborts any pending HTTP requests.

9
10
11
12
13
14
15
16
17
18
19
20
const createAssemblyRegex = /\/assemblies\/[0-9a-f]{32}/


describe('Mocked API tests', () => {
  afterEach(() => {
    nock.cleanAll()
    nock.abortPendingRequests() // Abort delayed requests preventing them from ruining the next test
  })


  it('should time out createAssembly with a custom timeout', async () => {
    const client = new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost' })
fork icon24
star icon56
watch icon0

9
10
11
12
13
14
15
16
17
18
19
20
const mockCluster = () => ({ });
const mockBullBuildQueue = (bull = {}) => new BullQueueClient(bull);


describe('server', () => {
  afterEach(() => {
    nock.abortPendingRequests();
    nock.cleanAll();
  });


  describe('GET /', () => {
fork icon10
star icon4
watch icon0

How does nock.abortPendingRequests work?

nock.abortPendingRequests is a function in the Nock library that aborts any pending HTTP requests.

When nock.abortPendingRequests is called, it performs the following steps:

  1. It iterates over all currently pending HTTP requests.
  2. For each request, it calls the abort method of the request object, causing the request to be aborted.

By aborting pending requests, nock.abortPendingRequests can help ensure that your application does not waste resources by continuing to send or receive data for requests that are no longer needed.

nock.abortPendingRequests can be useful in situations where you want to cancel all pending requests, such as when shutting down an application or canceling a long-running task.

Overall, nock.abortPendingRequests is a powerful tool for working with HTTP requests in Node.js, allowing developers to more easily manage the state of their applications.

157
158
159
160
161
162
163
164
165
166
afterEach(async () => {
  if (graphqlServer) {
    await graphqlServer.stop()
    graphqlServer = null
  }
  nock.abortPendingRequests()
})

it('Send federated schema to schema registry', async () => {
  const projectId = randomUUID()
fork icon0
star icon0
watch icon1

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const nock = require("nock");
const http = require("http");

// Intercept a GET request to http://example.com and delay the response by 10 seconds
nock("http://example.com").get("/").delay(10000).reply(200, "Hello, World!");

// Make a GET request to http://example.com
const req = http.get("http://example.com", (res) => {
  console.log("Got response:", res.statusCode);
});

// Abort any pending requests after 5 seconds
setTimeout(() => {
  nock.abortPendingRequests();
}, 5000);

In this example, we're using the nock library to intercept an HTTP request and delay the response by 10 seconds. We're then making a GET request to the intercepted URL using the http module. Since the response is delayed by 10 seconds, the request will be pending for some time. Finally, we're using setTimeout to call nock.abortPendingRequests after 5 seconds. This will abort any pending requests, including the delayed request we just made. When we run this code, we'll get output that looks something like this: yaml Copy code