10

I am learning how to test and using some examples as a guideline I am trying to mock a login post. The example used fetch for the http call but I using axios. This is the error I am getting

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

All of the answers to this error had to do with fetch, how do I do this with axios

./saga

const encoder = credentials => Object.keys(credentials).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(credentials[key])}`).join('&')

const postLogin = credentials => {
  credentials.grant_type = 'password'
  const payload = {
    method: 'post',
    headers: config.LOGIN_HEADERS,
    data: encoder(credentials),
    url: `${config.IDENTITY_URL}/Token`
  }
  return axios(payload)
}

function * loginRequest (action) {
  try {
    const res = yield call(postLogin, action.credentials)
    utils.storeSessionData(res.data)
    yield put({ type: types.LOGIN_SUCCESS, data: res.data })
  } catch (err) {
    yield put({ type: types.LOGIN_FAILURE, err })
  }
}

function * loginSaga () {
  yield takeLatest(types.LOGIN_REQUEST, loginRequest)
}

export default loginSaga

./login-test

const loginReply = {
  isAuthenticating: false,
  isAuthenticated: true,
  email: 'foo@yahoo.com',
  token: 'access-token',
  userId: '1234F56',
  name: 'Jane Doe',
  title: 'Tester',
  phoneNumber: '123-456-7890',
  picture: 'pic-url',
  marketIds: [1, 2, 3]
}

describe('login-saga', () => {
  it('login identity user', async (done) => {
    // Setup Nock
    nock(config.IDENTITY_URL)
      .post('/Token', { userName: 'xxx@xxx.com', password: 'xxxxx' })
      .reply(200, loginReply)

    // Start up the saga tester
    const sagaTester = new SagaTester({})

    sagaTester.start(loginSaga)

    // Dispatch the event to start the saga
    sagaTester.dispatch({type: types.LOGIN_REQUEST})

    // Hook into the success action
    await sagaTester.waitFor(types.LOGIN_SUCCESS)

    // Check the resulting action
    expect(sagaTester.getLatestCalledAction()).to.deep.equal({
      type: types.LOGIN_SUCCESS,
      payload: loginReply
    })
  })
})
dwjohnston
  • 7,389
  • 20
  • 81
  • 147
texas697
  • 4,171
  • 16
  • 56
  • 115

2 Answers2

2

You received the following error : Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL because you didn't call the done callback in your test.

socrateisabot
  • 837
  • 2
  • 10
  • 27
  • did this answer solve the issue ? Its is not clear where you are supposed to call done(). Calling this way might prematurely return done(). – tyskr Aug 25 '17 at 15:12
  • You are supposed to call done() manually when you're not returning a promise inside your test. – socrateisabot Aug 28 '17 at 09:44
1

Since you have specified a body ({ userName: 'xxx@xxx.com', password: 'xxxxx' }) in your nock mocking, it won't respond loginReply until it gets a post request with both given URL and body. But you don't send credentials with your LOGIN_REQUEST action and hence your axios request body( payload.data ) is always going to be empty. That's why your nock mocking doesn't reply within specified async timeout and jest give this timeout error.

To fix this you either have to remove the specified body in your nock setup or dispatch LOGIN_REQUEST action with credentials and change the specified body to match encoded credentials you set to payload.

Tharaka Wijebandara
  • 7,357
  • 1
  • 25
  • 44