ThinkingInBits
ThinkingInBits

Reputation: 11412

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.

Upvotes: 4

Views: 3592

Answers (2)

julian joseph
julian joseph

Reputation: 330

.send() is replaced by sending the post body as a second param after the url itself and .log doesn't work before .reply

Upvotes: 0

ThinkingInBits
ThinkingInBits

Reputation: 11412

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.

Upvotes: 12

Related Questions