nock does not intercept requests made by request/request-promise

Created on 24 Feb 2016  路  6Comments  路  Source: nock/nock

I was trying to mock a REST webservice that is consumed by a request-promise-powered client.

Unfortunately, nock does neither intercept the call nor is the recorder able to record the request. :(

Most helpful comment

Yep. Works like a charm. Must've been a different problem in my setup.

const nock = require('nock');
const rp = require('request-promise');

nock.disableNetConnect();

let body = {country: 'fr', email: 'francoise.[email protected]', userAgent: 'Gecko/1.2.3'};
let serverMock = nock('http://www.google.com').post('/index/test', body).reply(200, 'ok');

rp({
method: 'POST',
uri: 'http://www.google.com/index/test',
body: JSON.stringify(body),
headers: {
'content-type': 'application/json'
}
}).then(() => {
if (!serverMock.isDone()) {
console.log('test failed.');
} else {
console.log('test passed.');
}
});

All 6 comments

did you solve this?

Nope. - I think this was a false negative. I still didn't fix it, but couldn't reproduce it in a controlled setup. Will reopen this issue in case I succeed in doing so.

Yep. Works like a charm. Must've been a different problem in my setup.

const nock = require('nock');
const rp = require('request-promise');

nock.disableNetConnect();

let body = {country: 'fr', email: 'francoise.[email protected]', userAgent: 'Gecko/1.2.3'};
let serverMock = nock('http://www.google.com').post('/index/test', body).reply(200, 'ok');

rp({
method: 'POST',
uri: 'http://www.google.com/index/test',
body: JSON.stringify(body),
headers: {
'content-type': 'application/json'
}
}).then(() => {
if (!serverMock.isDone()) {
console.log('test failed.');
} else {
console.log('test passed.');
}
});

awesome, request-promise looks like a cool module too.

I probably know why you had that problem. I had the same issue in my unit tests and took me almost 30 minutes to understand what I was doing wrong:

In my unit test module, I had an afterEach block calling nock.restore(); instead of calling nock.cleanAll();, which was disabling nock instead of cleaning up the responses.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it鈥檚 related. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jhnlsn picture jhnlsn  路  7Comments

elliotf picture elliotf  路  6Comments

sidabs picture sidabs  路  6Comments

cristianszwarc picture cristianszwarc  路  6Comments

silvenon picture silvenon  路  7Comments