13

I am receiving following error

{ error: 
   { Error: Nock: No match for request {
     "method": "GET",
     "url": "http://localhost:3000/admin/orders/30075889/transactions.json",
     "headers": {
       "content-type": "application/json",
       "host": "localhost:3000"
     }
   } Got instead {
     "method": "GET",
     "url": "http://localhost:3000/admin/orders/30075889/transactions.json",
     "headers": {
       "content-type": "application/json",
       "host": "localhost:3000"
     }
   }

The url is as expected, not sure what's wrong, any pointer?

user269867
  • 2,280
  • 6
  • 35
  • 57
  • I am calling 2 apis, 1 is post and 1 is get. And doing Promise.all([1stApi, 2edApi]). But getting error as - Error: Nock: No match for request. When I put any 1 of the both in Promise.all(), then works fine. Any suggestion why this happens for 2 api calls? – MechaCode Jun 25 '20 at 06:48

3 Answers3

29

Use .log(console.log) to see the exact error message.

EX :

nock('https://test.org/sample')
.persist()
.log(console.log)
.get('/test')
.query({})
.reply(200, response);

When you use this and run the test, you will see something like this in the console

matching https://test.org/sample/test to GET https://test.org/sample/test with query({}): **true/false**.

If it says true, your request should be good. But if it says false, check both the requests and make sure they match.

CertainPerformance
  • 260,466
  • 31
  • 181
  • 209
user2092512
  • 343
  • 3
  • 11
  • 8
    Is it deprecated? `TypeError: nock(...).log is not a function` – Valu3 Mar 04 '21 at 12:19
  • Use the env variable `DEBUG=nock.*` instead of `.log(...)`, log is no longer supported. i.e.: `DEBUG=nock.* jest -i your/test/file.test.js` – ruedamanuel Apr 15 '21 at 22:18
14

Nock interceptors don't persist by default. For every request nock needs an interceptor. It looks like you only setup interceptor once and expect it to work for every request. If you want your interceptors to persist use .persist() option something like below.

var scope = nock('http://localhost.com')
  .persist()
  .get(/.*/)
  .reply(200, 'Nock all get requests!');
Meanteacher
  • 1,783
  • 3
  • 13
  • 42
3

you can display at any time prepared nocks, it's always a typo... ;)

console.log(nock.activeMocks());