0

Below is the sample code from redux document

describe('async actions', () => {
  afterEach(() => {
    nock.cleanAll()
  })

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

    const expectedActions = [
      { type: types.FETCH_TODOS_REQUEST },
      { type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something']  } }
    ]

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

    return store.dispatch(actions.fetchTodos())
      .then(() => { // return of async actions
        expect(store.getActions()).toEqual(expectedActions)
      })
  })
})

Why we nee to use nock for this unit test? I did not see any where use the data from nock in this sample code.

Dreams
  • 8,288
  • 10
  • 45
  • 71

2 Answers2

2

Nock is used to mock http requests - if you mock http request it means that your code doesn't perform real http requests to the server.

Nock (and any other http mocking library) overrides native http requests methods so that real http requests will be never sent. It has many benefits - for example you don't have to wait for actual server response because mocked request returns response in no time and of course your test are independent of server. You can focus on testing application code and don't worry about server - even if server doesn't work you test can be run.

You don't have to explictly use data returned by mocked request if you don't need to test it - the main reason of using nock in your code sample is to prevent actual http request to the server that FETCH_TODOS_REQUEST action would normally sent. Besides, even if mocked response data is not explicily used in tests it's probably used in the application code (probably FETCH_TODOS_SUCCESS action expects todos array to be returend) so you have to mock response data so that your application gets data it expects.
If nock wasn't used the test would take much more time because real http request to the server would be sent.

Bartek Fryzowicz
  • 6,464
  • 18
  • 27
  • Thanks a lot. If I use query Ajax to call my api, will nock also intercept it? or nock only intercept fetch call? – Dreams Mar 31 '17 at 09:33
  • You're welocme. `nock` works only in node env - not in browser env. Redux doc examples uses Jest (which runs test in node env) as test runner and `isomorphic-fetch` to make http request which supports also node env so `nock` works but I'm afraid that if you try to use browser API to make AJAX call it will not work - `nock` doesn't override browser API methods. Please check also this SO answer: http://stackoverflow.com/a/40270833/7518718 – Bartek Fryzowicz Mar 31 '17 at 10:15
0

Mainly because in this test we're interested in the actions that get produced by actions.fetchTodos(). This action will make a call to the /todos endpoint, thus returning actions with some data. Since we're just interested in the data contained in the actions, we just mock it.

Nock internally intercepts the real fetch call to /todos and returns a successful 200 code, making it possible for the redux store to continue.

The data you're looking for is

{ todos: ['do something']  }

This is mocked and also expected later on

Ignacio
  • 1,056
  • 8
  • 17