Search code examples
node.jsintegration-testingnock

Mocking with Nock, mock only a specific route with the same host


I am using Nock ( https://github.com/node-nock/nock ) to mock an underly service called by an endpoint that I need to test in my application. In the implementation of the endpoint, I am calling this underly service multiple time like that :

I want to know if it's possible to only mock ONE of those. Let's say this one : http://samehost/specificrsc2/

Currently i am not able to achieve that. Because I get an error like this :

'FetchError: request to http://samehost/specificrsc1/ failed, reason: 
 Nock: No match for request {\n  "method": "GET",\n  "url": 
"http://samehost/specificrsc1/",

Thats how i am mocking the underly service :

const mockUnderlyCall = nock('http://samehost');
mockUnderlyCall.get('/samehost/specificrsc1').reply(200, mockData)

I also try :

const mockUnderlyCall = nock('http://samehost/samehost/specificrsc1');
mockUnderlyCall.get('').reply(200, mockData)

Thank you !


Solution

  • I want to know if it's possible to only mock ONE of those

    Yes, use the allowUnmocked option.

    const mockUnderlyCall = nock('http://samehost', {allowUnmocked: true});
    

    Note, this is listed in the documentation for nock.