Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking superagent post request with nock not matching

I cannot for the life of me get nock to work with a simple superagent post request. Here is both my superagent and nock configuration.

superagent:

request
  .post('https://test.com/api/login')
  .send({
    email: '[email protected]',
    password: 'testpassword'
  })
  .end((err, res) => {
    if (err) {
      console.log(err);
    }
  });

nock:

nock('https://test.com')
  .post('/api/login')
  .send({
    email: '[email protected]',
    password: 'testpassword'
  })
  .reply(200, {
    id: 1,
    token: 'abc'
  });

I'm receiving the following error from nock:

{ [Error: Nock: No match for request POST https://test.com/api/login {"email":"[email protected]","password":"testpassword"}] status: 404, statusCode: 404, response: undefined }

Another weird aspect is that this error is being logged in my superagent response handler. So I know the call is being made and intercepted.

like image 931
ThinkingInBits Avatar asked Dec 15 '22 09:12

ThinkingInBits


1 Answers

Ok -- figured it out. After digging through the docs a bit more I found the .log function. I chained my nock config like so

nock('https://test.com')
    .log(console.log)...

and it turns out the request bodies were not matching.

like image 95
ThinkingInBits Avatar answered Mar 15 '23 16:03

ThinkingInBits