How to test the saga reaction axioms

I am learning how to test and use some examples as a guide. I'm trying to make fun of the login. The example uses fetch to call http, but I use axios. This is the error I get

Timeout - Async callback is not called within the timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL

All answers to this error are related to extracting how to 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 

./ Enter 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 }) }) }) 
+10
javascript reactjs chai jestjs jest nock
source share
2 answers

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

+2
source share

Since you have the body ( { userName: 'xxx@xxx.com', password: 'xxxxx' } ) in your nock mocking, it will not respond loginReply until it receives an email request with both the given URL and with the body. But you are not sending credentials with your LOGIN_REQUEST action, and therefore, the axios request body ( payload.data ) will always be empty. This is why your nock mocking does not respond within the specified async timeout and jest gives this timeout error.

To fix this, you need to either delete the specified object in the nock settings, or send the LOGIN_REQUEST action with credentials and change the specified authority to match the encoded credentials set to payload .

+1
source share