How to use the nock.emitter function in nock

To help you get started, we’ve selected a few nock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github unlock-protocol / unlock / unlock-js / src / __tests__ / helpers / nockHelper.js View on Github external
this.nockScope = nock(endpoint, { encodedQueryParams: true })

    this.recording = record
    if (record) {
      nock.recorder.rec({
        output_objects: true,
      })
    }

    this.anyRequestSetUp = false
    this.debug = debug
    // ethers hard-codes this value, see https://github.com/ethers-io/ethers.js/issues/489
    this._rpcRequestId = 42
    this._noMatches = []

    nock.emitter.on('no match', (clientRequestObject, options, body) => {
      this._noMatches.push(body)
      if (debug) {
        if (!this.anyRequestSetUp) {
          console.log(
            new Error('No mocks have been set up, but a request was made!')
          )
        }
        if (!body) {
          console.log(
            new Error(
              'no body? (is there a jest.fakeTimers call? you must reset mocks' +
                ', see https://github.com/sapegin/jest-cheat-sheet#clearing-and-restoring-mocks'
            )
          )
        }
        console.log(`NO HTTP MOCK EXISTS FOR THAT REQUEST\n${body}`)
github nock / nock / types / tests.ts View on Github external
hostname: 'localhost',
  path: '/mockedResource',
})
nock.removeInterceptor({
  hostname: 'localhost',
  path: '/login',
  method: 'POST',
  proto: 'https',
})

const interceptor = nock('http://example.test').get('somePath')
nock.removeInterceptor(interceptor)

// Events
/// Global no match event
nock.emitter.on('no match', (req: any) => {})

// Nock Back
/// Setup
nock.back.fixtures = '/path/to/fixtures/'
nock.back.setMode('record')

/// Usage
const before = (def: nock.Definition) => {
  def.options = def.options || {}
  def.options.filteringScope = (scope: string) => {
    return /^https:\/\/api[0-9]*.example.test/.test(scope)
  }
}
const after = (scope: nock.Scope) => {
  scope = scope.filteringRequestBody((body: string): string => {
    return body
github balena-io / balena-cli / tests / balena-api-mock.ts View on Github external
public done() {
		// scope.done() will throw an error if there are expected api calls that have not happened.
		// So ensures that all expected calls have been made.
		this.scope.done();
		// Remove 'no match' handler, for tests using nock without this module
		nock.emitter.removeListener('no match', this.handleUnexpectedRequest);
		// Restore unmocked behaviour
		nock.cleanAll();
		nock.restore();
	}
github magda-io / magda / magda-sleuther-broken-link / spec / sleuther.spec.ts View on Github external
// Set up an FTP server that will respond in line with test data.
    ftp = setupFtp();

    // Mess with node DNS so that a call to any host that starts with FTP actually gets resolved to our local FTP server.
    sinon.stub(dns, "lookup").callsFake((hostname, options, callback) => {
      if (hostname.startsWith("ftp")) {
        callback(null, "127.0.0.1", 4);
      } else {
        originalDns(hostname, options, callback);
      }
    });

    sinon.stub(console, "info");

    nock.emitter.on("no match", onMatchFail);
  });
github wikimedia / restbase / test / features / lists.js View on Github external
after(() => {
        nock.emitter.removeListener('no match', unmockedListener);
        return server.stop();
    });
github wikimedia / restbase / test / features / lists.js View on Github external
        .then(() => nock.emitter.on('no match', unmockedListener));
    });
github balena-io / balena-cli / tests / balena-api-mock.ts View on Github external
constructor() {
		nock.cleanAll();

		if (!nock.isActive()) {
			nock.activate();
		}

		this.scope = nock(BalenaAPIMock.basePathPattern);

		nock.emitter.on('no match', this.handleUnexpectedRequest);
	}
github magda-io / magda / magda-sleuther-broken-link / spec / sleuther.spec.ts View on Github external
after(() => {
    dns.lookup.restore();
    (console.info as any).restore();

    ftp.close();
    nock.emitter.removeListener("no match", onMatchFail);
  });