How to use the cleanAll function from nock

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

nock.cleanAll is a function in the Nock library that removes all the mocked request interceptors.

4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
    pipelineFactoryMock.get.resolves(pipelineMock);
    nock.disableNetConnect();
});

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

it('returns 200 for a step that exists', () => {
fork icon161
star icon969
watch icon65

77
78
79
80
81
82
83
84
85
86
  const jestTestFile = fs.readdirSync(__dirname).filter(name => name.startsWith('jest-'))
  jestTestFile.forEach((testFile) => {
    delete require.cache[require.resolve(path.join(__dirname, testFile))]
  })
  delete require.cache[require.resolve(path.join(__dirname, 'env.js'))]
  nock.cleanAll()
  return agent.close({ ritmReset: false, wipe: true })
})
beforeEach(function () {
  process.env.DD_API_KEY = 'key'
fork icon241
star icon444
watch icon464

+ 17 other calls in file

How does nock.cleanAll work?

When using the Nock library to mock HTTP requests, you can use nock.cleanAll to remove all mocked request interceptors. Mocked request interceptors are created using the nock function and can be used to intercept requests and return mock responses. However, sometimes you may want to remove these interceptors, for example if you need to start over with a clean slate or to ensure that the interceptors don't interfere with other tests. The nock.cleanAll function removes all the mocked request interceptors that have been set up with the nock function. This function does not take any arguments and simply removes all the interceptors. Here's an example of using nock.cleanAll: javascript Copy code {{{{{{{ const nock = require('nock'); // Set up some mock interceptors nock('https://example.com') .get('/foo') .reply(200, { success: true }); // Call an API that triggers the interceptor const response = await fetch('https://example.com/foo'); // Clean up the interceptors nock.cleanAll(); // Set up some new interceptors nock('https://example.com') .get('/bar') .reply(200, { success: true }); // Call another API that triggers the new interceptor const response2 = await fetch('https://example.com/bar'); In this example, we first set up a mocked request interceptor using the nock function, which intercepts all GET requests to https://example.com/foo and responds with a success status and a JSON object. After we call an API that triggers the interceptor, we use nock.cleanAll() to remove all the mocked request interceptors. We then set up a new interceptor for https://example.com/bar using the nock function, and make another API call that triggers this new interceptor. Overall, nock.cleanAll is a useful function for removing mocked request interceptors when using the Nock library for mocking HTTP requests.

195
196
197
198
199
200
201
202
203
204
  peerDataCopy = JSON.parse(JSON.stringify(testdata.getPeerInfo()));
  transactionDataCopy = JSON.parse(JSON.stringify(testdata.getTransaction()));
  unspentDataCopy = JSON.parse(JSON.stringify(testdata.listUnspent()));
});

beforeEach(() => nock.cleanAll());
afterAll(() => nock.restore());
beforeAll(() => {
  if (!nock.isActive()) nock.activate();
  nock.enableNetConnect();
fork icon6
star icon1
watch icon0

67
68
69
70
71
72
73
74
75
76
77
78
79
};


test('HTTP engine', function (tap) {
  tap.before(async () => await updateGlobalObject());


  tap.beforeEach(() => nock.cleanAll());


  tap.test('HTTP engine interface', function (t) {
    const engine = new HttpEngine(script);
    const ee = new EventEmitter();
fork icon436
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const nock = require("nock");
const fetch = require("node-fetch");

// Set up some mock interceptors
nock("https://example.com").get("/foo").reply(200, { success: true });

// Call an API that triggers the interceptor
const response = await fetch("https://example.com/foo");

// Clean up the interceptors
nock.cleanAll();

// Set up some new interceptors
nock("https://example.com").get("/bar").reply(200, { success: true });

// Call another API that triggers the new interceptor
const response2 = await fetch("https://example.com/bar");

In this example, we first set up a mocked request interceptor using the nock function, which intercepts all GET requests to https://example.com/foo and responds with a success status and a JSON object. After we call an API that triggers the interceptor, we use nock.cleanAll() to remove all the mocked request interceptors. We then set up a new interceptor for https://example.com/bar using the nock function, and make another API call that triggers this new interceptor. Overall, nock.cleanAll is a useful function for removing mocked request interceptors when using the Nock library for mocking HTTP requests.