4

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: 'test@test.com',
    password: 'testpassword'
  })
  .end((err, res) => {
    if (err) {
      console.log(err);
    }
  });

nock:

nock('https://test.com')
  .post('/api/login')
  .send({
    email: 'test@test.com',
    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":"test@test.com","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.

ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82

2 Answers2

12

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.

ThinkingInBits
  • 10,792
  • 8
  • 57
  • 82
  • Saved a heaps of time – Shaolin Sep 27 '16 at 06:36
  • It looks like the interface for nock has changed (related to post). Instead of chaining the call to ".post("/api").send({..})", you now seem to need to do ".post("/api", {..})". Hope this helps - I'm using nock v8. – RichS Nov 01 '16 at 14:48
0

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

Buddy
  • 10,874
  • 5
  • 41
  • 58
julian joseph
  • 330
  • 1
  • 4
  • 18