How to use nock

Comprehensive nock code examples:

How to use nock.times:

658
659
660
661
662
663
664
665
666
667
    defaults: {}
});

let threw = false;

//tried nock.times(5) but it does not update nock.pendingMocks().length
for (let i = 0; i < 5; i++) {
    nock("https://eg-ingress.adobe.io")
        .matchHeader('Authorization',`Bearer ${FAKE_ACCESS_TOKEN}`)
        .matchHeader('x-ims-org-id',FAKE_ORG_ID)

How to use nock.define:

75
76
77
78
79
80
81
82
83
84
    : (scope) => scope

  // turn the json data into real nocks and return them
  const nocks = this.#snapshot[key]
    .map((scope) => load(scope))
  const scopes = nock.define(nocks)
  return scopes
}

start (name, options = {}) {

How to use nock.emitter:

25
26
27
28
29
30
31
32
33
34
35
};


function initNock() {
  nock.disableNetConnect();
  const logRequest = (r) => console.log(`No match: ${r.path}, method: ${r.method}, host: ${r.options.host}`);
  nock.emitter.on('no match', (req) => {
    logRequest(req);
  });
}

How to use nock.recorder:

37
38
39
40
41
42
43
44
45
46

[_saveState] ({ key, clean }) {
  if (!this.#snapshot[key]) {
    this.#snapshot[key] = []
  }
  const recordings = nock.recorder.play()
    .map((scope) => clean(scope))
  this.#snapshot[key].push(...recordings)
  nock.recorder.clear()
}

How to use nock.back:

2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
    const ecPL = testCtxArtifact.toPublishableLibrary();
    expect(_.isEqual(difference(ecPL, testExpandedContext), {})).to.equal(true);
  });
});


const nockBack = nock.back;
nockBack.fixtures = path.join(__dirname, 'fixtures');
nockBack.setMode('record');


describe('CQL to ELM tests', () => {

How to use nock.isDone:

2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
  };
});

// after each test, check and clean nock
afterEach(() => {
  nock.isDone();
  nock.cleanAll();
});

it('should send the right translation parameters to the CQL Translation Service', done => {

How to use nock.abortPendingRequests:

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' })

How to use nock.disableNetConnect:

4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
    stepMock = getStepMock(testStep);
    stepFactoryMock.get.withArgs({ buildId: id, name: step }).resolves(stepMock);
    buildFactoryMock.get.resolves(buildMock);
    eventFactoryMock.get.resolves(eventMock);
    pipelineFactoryMock.get.resolves(pipelineMock);
    nock.disableNetConnect();
});

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

How to use nock.enableNetConnect:

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')

How to use nock.activate:

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

How to use nock.restore:

2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
  nock.disableNetConnect();
});

// after the tests, re-enable network connections
after(() => {
  nock.restore();
  nock.enableNetConnect();
});

// before each test, setup the commonly used data

How to use nock.cleanAll:

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', () => {