nock is not blocking the request.
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:9 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I cannot make it work with
nock
(I checked here too). But it works fine with fetchMockIs it a bug from
nock
?Nock is not working in Redux example using Jest for me too. request always happens. I also tried recording using:
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