question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why nock throw error `Nock: No match for request` when repeating request

See original GitHub issue

Tests case:

nock('https://xxxx.com').get('/').times(1).reply(200, { mock: 1 });

// first times: print { mock: 1 }
// second and third times: throw error `Nock: No match for request`
for (let i = 0; i < 3; i++) {
  const {data} = await axios({
    url: 'https://xxxx.com/',
    method: 'GET',
  });
  console.log(data);
}

In README, means you can get real response from url you specified but not an error.

But in your tests cases: It should get error when repeating.

Version

  • Nodejs: v12
  • Nock: 13.0.7

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mastermattcommented, Mar 6, 2021

Agreed, the docs need updating. PRs welcome!

@marcusNumminen to achieve your original, expected behavior the allowUnmocked flag needs to be passed when creating a Scope. https://github.com/nock/nock#allow-unmocked-requests-on-a-mocked-hostname

nock('https://xxxx.com', {allowUnmocked: true}).get('/').times(1).reply(200, { mock: 1 });

// first times: print { mock: 1 }
// second and third times attempt live calls over the network, assuming `nock.disableNetConnect()` wasn't called first
for (let i = 0; i < 3; i++) {
  const {data} = await axios({
    url: 'https://xxxx.com/',
    method: 'GET',
  });
  console.log(data);
}
1reaction
taozi0818commented, Mar 4, 2021

Is this example https://github.com/nock/nock#repeat-response-n-times correct then? As I interpreted the example the fifth call should return the result from the “real” call to http://zombo.com/

No, you will get an error from the fifth call. See this tests case https://github.com/nock/nock/blob/main/tests/test_repeating.js README and the logic of code do not match.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Nock: No match for request - Stack Overflow
I am calling 2 apis, 1 is post and 1 is get. And doing Promise.all([1stApi, 2edApi]). But getting error as - Error: Nock:...
Read more >
[Solved]-Error: Nock: No match for request-node.js
It looks like you only setup interceptor once and expect it to work for every request. If you want your interceptors to persist...
Read more >
Nock - JavaScripting
If no request headers are specified for mocking then Nock will automatically skip matching of request headers. Since the host header is a...
Read more >
No match error when testing an Express.js controller doing ...
The solution was to append the call to the .persist() method between nock('http://backend') and get('/user/details') as shown in code below:
Read more >
HTTP Server mocking for Node.js
function activate() { if(originalClientRequest) { throw new Error('Nock already ... Enable/Disable real HTTP request As default, if you do not mock a host, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found