0

I often face this error (Error: Nock: No match for request) using Nock, but I find it hard to quickly understand what doesn't match in order to adjust.

I used to use nock(apiUrl).persist().log(console.log)(link) that was doing the job pretty well. But it's been deprecated and I've recently updated the dependancy. Therefore I need to adapt, yet I find it hard to grasp what info is interesting for my debugging.

How to get something similar to Jest, obviously comparing what was expected, and what was actually called for quick and straightforward debugging ?

A Mehmeto
  • 93
  • 2
  • 13

2 Answers2

1

Nock uses the Debug lib for debugging. https://github.com/nock/nock#debugging

It's not great, but it gives you insight usually into what is going wrong and we plan on making the output better (at some point).

Matt R. Wilson
  • 5,770
  • 5
  • 25
  • 44
  • I finally found out that I had a typo on the "nocked" query parameter, having a camelCase key instead of snake_case key in the query object. It was hard to see. I haven't found it with the debug that was showing to much info in my opinion. If I could make one suggestion, it would be wonderful to have an output such as Jest, that straighforwardly compare what was expected with what was received. Cheers, thanks for the response and documentation reference. – A Mehmeto Feb 26 '21 at 15:39
0

For those that might help, here is how you must read the error :

  console.log src/repositories/repository.js:137
    Error: Nock: No match for request {
      "method": "PATCH",
      "url": "http://random.example.ci.indb:8080/v1/subscriptions?user_id=c141db67-2e85-46e4-b07e-95d2e913e05d",
      "headers": {
        "accept": "application/json, text/plain, */*",
        "content-type": "application/json;charset=utf-8",
        "x-forwarded-proto": "https",
        "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJtam9sbG5pci50ZXN0LmluZGIiLCJzdWIiOiI3MzIyNDJiNy03YmUxLTRmOWItYjY3MC1jNGVkZmNhZTExMzIiLCJzdWJsaXZlIjo0MiwiaWF0IjoxNjE0NjMyMDY2LCJleHAiOjE2MTcyMjQwNjZ9.yS58zjEYj4Yx4iSga0w-NFA1QbdznD1vLGAUhEcjyXo",
        "user-agent": "axios/0.21.1",
        "content-length": 146
      },
      "body": "{\"user_id\":\"c141db67-2e85-46e4-b07e-95d2e913e05d\",\"conditions_accepted_date\":\"Mon Mar 01 2021 21:14:22 GMT+0100 (Central European Standard Time)\"}"
    }

The error print into the console what your code is actually calling. You need to compare those parameters thoroughly with what you've given in your test's Nock mock. In my case it was due to a mismatch with the date input.

The other infos seems useless.

A Mehmeto
  • 93
  • 2
  • 13