Jbeat
Jbeat

Reputation: 151

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 !

Upvotes: 4

Views: 2514

Answers (2)

john k
john k

Reputation: 6615

Nock allows you to separate the hostname from the individual routes/endpoints. Unless I'm completely misunderstanding your question, you just specify your paths separately.

Nock documentation is here: https://github.com/nock/nock?tab=readme-ov-file#specifying-path

Suggested Code:

 nock('http://samehost')
    .get('/pvi/api/specificrsc3/')
    .reply();

Upvotes: 1

morganney
morganney

Reputation: 13560

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.

Upvotes: 4

Related Questions