4

I am using nock.back as part of mocha tests in my project.

Even though I have setup nock.enableNetConnect, nock.back still records outgoing calls to the domain.

const nock = require('nock')
const path = require('path')

nock.back.fixtures = path.join(__dirname, 'fixtures')

// use recorded nocks, and record new nocks - don't remove
nock.back.setMode('record')
nock.enableNetConnect(/(vault|localhost|schema-registry)/)
const noBodyMatching = scope => {
  scope.filteringRequestBody = (_body, recordedBody) => recordedBody
  nock.enableNetConnect(/(vault|localhost|schema-registry)/)
}

const defaultOptions = {
  before: noBodyMatching
}

const nockBack = (fixture, options) => {
  let nockDone
  beforeEach(async function() {
    //  eslint-disable-next-line
    ;({ nockDone } = await nock.back(fixture, {
      ...defaultOptions,
      ...options
    }))
  })

  afterEach(function() {
    nockDone()
  })
}

module.exports = {
  nockBack,
  defaultOptions
}

I still get errors like following:

No match for request {\n  \"method\": \"GET\",\n  \"url\": \"http://schema-registry:3000/find-schema-by-routing-key/event.serviceability.offers.retry.queued\"

Looking at some existing/closed issues, I found following but didn't have any luck: https://github.com/nock/nock/issues/484#issuecomment-191822034

Would someone know how to get nock.back to ignore certain domains? or is this not possible?

Progman
  • 16,827
  • 6
  • 33
  • 48
akshah123
  • 708
  • 1
  • 5
  • 10

2 Answers2

0

Here is how I set up nock back to record only one domain:

const { nockDone } = await nock.back('export.json', {
    before: (def) => {
        def.scope = 'https://somedomain.com';
    },
});
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
0

Took awhile but this is what worked for me:

const NOCK_OPTIONS: nock.BackOptions = {
  afterRecord: (list) => list.filter((value) =>
    !(value.scope as string).includes(process.env.TMDB_API_URL)
  )
}

const { nockDone } = await nock.back('export.json', NOCK_OPTIONS)

Tried most everything else I found and this is what worked for me.

Essentially afterRecord runs before it writes the file so it's a bit of an oddly named method IMO.

Also I have a feeling Nock struggles with API consistency so fwiw I'm on 13.3.3 in case this feels like an estranged brother from the rest of the API family.

  • Anand Malik