nock-record icon indicating copy to clipboard operation
nock-record copied to clipboard

Feature ideas

Open edorivai opened this issue 6 years ago • 4 comments

  • [ ] Allow setting mode through environment variables
  • [x] Remove jest dependency (should I rename the module then?)

edorivai avatar Apr 08 '18 15:04 edorivai

Could use a quick URL rewriter -- in this case I've got a localhost item responding, but would like the mock to pretend like it's actually the default URL.

Something like rewriteUrl( async fn (input: string): Promise), so I can do return "http://localhost/" + path;

This would still serialize into the nock fixture as the original url.

Downchuck avatar Jul 10 '19 17:07 Downchuck

For integration testing, I want to send requests to a HTTTP API which will in turn send requests that I want to mock to other servers. I need a way to specify that the requests to 127.0.0.1 or similar are not to be recorded and mocked..

hallvors avatar Jan 14 '21 14:01 hallvors

@hallvors That makes total sense, but unfortunately it doesn't look like Nock Back supports ignoring certain hosts. We're just wrapping Nock Back here, so we're kinda limited by what they expose.

Link to the docs: https://github.com/nock/nock#nock-back.

edorivai avatar Jan 14 '21 15:01 edorivai

@hallvors That's how I made it in my code:

import * as nock from 'nock';
import { setupRecorder } from 'nock-record';

export const record = async fixtureName => {
  const nockRecord = setupRecorder({
    fixturePath: __dirname + '/nock-tapes',
    mode: 'record',
  });

  const { completeRecording, ...rest } = await nockRecord(
    fixtureName,
    {
      after: () => {
        nock.enableNetConnect('127.0.0.1');
      },
      afterRecord: outputs => outputs.filter(o => !o.scope.match(/127.0.0.1/)),
    },
  );

  return {
    completeRecording: () => {
      nock.restore();
      nock.cleanAll();
      nock.enableNetConnect();
      completeRecording();
    },
    ...rest,
  };
};

If I get it right, nock.back just disables net connect globally. So I just ignore records from localhost and enable localhost network right after setup.

@edorivai Maybe there's a way to implement such feature using a similar approach? I guess this is a very useful thing. I expected that nock.back supports this out of the box.

heleg avatar Jan 15 '21 20:01 heleg