4

My nock call looks like as below

app_url='myshop.app.com'
result = nock(app_url, {
            reqheaders: {
                 "content-type": "application/json",
                 'authorization': 'Basic Auth'
            }
          })
        .get('/admin/products.json?collection_id=10001&limit=250&fields=id')    
        .reply(200, {
                "products": [
                { "id": 101},
                {"id": 102},
            ]
        });

Resolve :

(node:1378) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Nock: No match for request { .  

But now,

==========updated=============

I have updated my call which is not throwing an error but now it is not intercepting the request....It is still hitting shopify to fetch the data

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
user269867
  • 3,266
  • 9
  • 45
  • 65

3 Answers3

5

Just add flag in scope { allowUnmocked: true }

nock('https://host.com', { allowUnmocked: true })
    .post(`/path`)
    .reply(200, {answer: "any"});
  • 1
    This is not right, it should be the other way around. You should not allow unmocked URL. – LEMUEL ADANE Nov 17 '19 at 07:22
  • For this case, check if there are no unnecessary body params being passed incorrectly: Example: `.post(\`/path/${id}\`, unecessaryBody)` should actually just be `.post(\`/path/${id}\`)` or the opposite, it requires a body param and you are not passing – Roni Castro Oct 28 '22 at 20:09
1

This happens when a nock matching the URL being hit is not found. The url being hit is

https://myshop.app.com/admin/products.json?collection_id=201&limit=10&fields=id

as seen in the error message. The URLs you are nocking do not correspond to it.

Something like this should work.

app_url = "https://myshop.app.com/"
result = nock(app_url, {
        reqheaders: {
            "content-type": "application/json",
        }
    })
    .get('/admin/products.json?collection_id=201&limit=10&fields=id').
    .reply(200, {
        "products": [{
            "id": 101
        }, {
            "id": 102
        }, ]
    });

More details about the exact way to nock can be found in the Nock Documentation.

0

The nocked URL must be exactly the same as the URL that would be executed by Node in order to avoid the 'Nock: No match for request' error. So all you need to do is:

nock('https://host.com')
 .log(console.log) // log the mocked URL so that you will know if they're 
                   // the same or not.
 .post(`/path`)
 .reply(200, {answer: "any"});
LEMUEL ADANE
  • 8,336
  • 16
  • 58
  • 72