What am I missing here?
[~/tmp]$ cat test.js
const Wreck = require('wreck');
const nock = require('nock');
const nockBack = nock.back;
nock.enableNetConnect();
nock.enableNetConnect(/canihaz/);
nockBack.fixtures = './tmp';
nockBack.setMode('record');
Wreck.get('https://canihazip.com/s', (err, res, body) => {
var p = require('purdy');
p(err);
p(res);
p(body);
});
[~/tmp]$ node test.js
NetConnectNotAllowedError {
name: 'NetConnectNotAllowedError',
code: 'ENETUNREACH',
message: 'Client request error: Nock: Not allow net connect for "canihazip.com:443/s"',
trace: [
[0] {
method: 'GET',
url: 'https://canihazip.com/s'
}
],
isBoom: true,
isServer: true,
data: null,
output: {
statusCode: 502,
payload: {
message: 'Client request error: Nock: Not allow net connect for "canihazip.com:443/s"',
statusCode: 502,
error: 'Bad Gateway'
},
headers: {}
},
reformat: [Function]
}
undefined
undefined
Move nock.enableNetConnect(); below the nockBack fixtures and setMode lines. Then it should work. There's also no need for multiple enableNetConnect - the first is permissive of all netConnects, and the second just limits it, so just go with one or the other.
Let me know if this works for you!
Thanks that works. Is there any reason this wouldn't be enabled by default for this type of recording ?
Yes. It looks like the docs don't actually make it clear that 'enableNetConnect' is only the default for two of the available modes.
var Modes = {
wild: wild, //all requests go out to the internet, dont replay anything, doesnt record anything
dryrun: dryrun, //use recorded nocks, allow http calls, doesnt record anything, useful for writing new tests (default)
record: record, //use recorded nocks, record new nocks
lockdown: lockdown, //use recorded nocks, disables all http calls even when not nocked, doesnt record
};
So, on record and lockdown, you need to explicitly state the http calls you want enabled. For the other two, they will go through.
Want to take a look at #1200 and see if it looks good to you? If so, I think it would be great if you could add a PR to that section specifying that 'enableNetConnect' only works for two of the modes by default. That way, this issue shouldn't happen again for the next person. What do you think?
I never had a chance to get back to this but in my example here, I don't see anything actually being recorded in ./tmp. Am I missing something else too?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.
Most helpful comment
Move
nock.enableNetConnect();below the nockBack fixtures and setMode lines. Then it should work. There's also no need for multipleenableNetConnect- the first is permissive of all netConnects, and the second just limits it, so just go with one or the other.Let me know if this works for you!