nock-record
nock-record copied to clipboard
Feature ideas
- [ ] Allow setting
mode
through environment variables - [x] Remove jest dependency (should I rename the module then?)
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
This would still serialize into the nock fixture as the original url.
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 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.
@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.