Error: Nock: No Match for Request

I am receiving following error

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

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

1

5 Answers

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

EX :

nock(')
.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  to GET  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.

2

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(')
  .persist()
  .get(/.*/)
  .reply(200, 'Nock all get requests!');
1

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

console.log(nock.activeMocks());

(debuger in webstorm sucks and nock is undefined)

sometimes can be a encodedQueryParams: true option the problem

encoding query params is default behaviour these days, works well without such an option

Mostly a typo. In my case I had to add a / before the string in get method. .get('/list')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest