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.

nock is not blocking the request.

See original GitHub issue

I follow this example from redux.org to test async action. I face the issue that nock is not blocking the request. Here is part of the code in that sample

// action.js
function fetchTodos() {
  return dispatch => {
    return fetch('http://example.com/todos')
      .then( res => console (res) )
      .catch( err=> console(err) )
  }
}

// test.js
it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', () => {
    nock('http://example.com/')
      .get('/todos')
      .reply(200, { body: { todos: ['do something'] }})

    const store = mockStore({ todos: [] })

    return store.dispatch(actions.fetchTodos())
      .then(() => { 
             console.log('working')
      })
  })

What happen during the run is, Nock is not mocking the request, function fetchtodos actually did a real request in this case. What could be wrong here?

Here is a demo: https://github.com/craigcosmo/react-redux-test

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasthiebaudcommented, Oct 17, 2016

I cannot make it work with nock (I checked here too). But it works fine with fetchMock

fetchMock.post('*', { errors: [{title: 'Error', detail: 'Some random error'}] });

Is it a bug from nock ?

1reaction
niksajanjiccommented, Oct 6, 2016

Nock is not working in Redux example using Jest for me too. request always happens. I also tried recording using:

nock.recorder.rec()

And even after copying what recorder consoled out it still didn’t intercept it. The example can be found on this link:

http://redux.js.org/docs/recipes/WritingTests.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nock not intercepting http requests - Stack Overflow
Nock works by overriding Node's http.request function. Also, it overrides http.ClientRequest too to cover for modules that use it directly.
Read more >
API mock testing with Nock - CircleCI
This tutorial will show you how to mock HTTP requests from an API so that you can test endpoints without really calling them...
Read more >
nock - npm.io
By default, any requests made to a host that is not mocked will be executed normally. If you want to block these requests,...
Read more >
nock - Devpost
If no request headers are specified for mocking then Nock will automatically skip matching of request headers. Since host header is a special...
Read more >
Why did Nock not record all the api requests? | BytesMatter
tl;dr Nock overrides http(s).request functions, which needs to be done before other code stores a reference to it.
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