user269867
user269867

Reputation: 3972

Error: Nock: No match for request

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?

Upvotes: 34

Views: 61183

Answers (5)

nock.activeMocks()

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)

Upvotes: 14

Nagibaba
Nagibaba

Reputation: 5378

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

Upvotes: 2

sometimes can be a encodedQueryParams: true option the problem

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

Upvotes: 0

user2092512
user2092512

Reputation: 439

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.

Upvotes: 32

user4170419
user4170419

Reputation:

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!');

Upvotes: 32

Related Questions