axios / moxios

Mock axios requests for testing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

canoqb10 opened this issue · comments

I have a test with moxios and those results are an error, I read the docs and I wrote the same form that of these describe in how define an async test,the error is, I read also the docs of jasmine. Do you knows what happens?

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at node_modules/jest-jasmine2/build/queue_runner.js:65:21
at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:523:19)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
Test

`import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import multi from 'redux-multi';
import promise from 'redux-promise';

import moxios from 'moxios';

import LocalStorageMock from '../../layouts/mock-localstorage'
import { password, leavePassword, restorePass, types } from '../../actions/password-actions';

//const middlewares = [multi];
const middlewares = [multi,thunk];
//const middlewares = [multi,promise];
const mockStore=configureStore(middlewares);

const baseUrl = 'localhost/project';
const REGISTER_ENDPOINT = 'account/password';
const RESTORE_ENDPOINT = 'account/reset_password/init';

let originalTimeout;
beforeEach(() =>{
//originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;//that not works
//jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;//that not works
moxios.install();
});
afterEach(() => {
localStorage.clear();
moxios.uninstall();
//jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;//that not works
});

global.localStorage= new LocalStorageMock('auth');

it('when user set password isnt valid', async(done)=>{ //it('when user set password isnt valid',()=>{
const store = mockStore({password:{}});
const path = baseUrl + REGISTER_ENDPOINT;
moxios.stubRequest(path,{
status: 404,
response:{
message:'BAD_REQUEST'
}
});

const values = { 
    newPassword: 'password',
    email: 'emailo@mailinator.com',
    key: 'eXyGsaerFAS213das&%ASDas'
};


store.dispatch(password(values));
const actions= store.getActions();


expect(actions.length).toBe(2);
expect(actions[0].type).toBe(types.SETPASS_STARTED);
expect(actions[1].type).toBe(types.SETED_PASS);
expect(actions[1].payload).not.toBe(null || undefined);

await actions[1].payload.then((response)=>{
expect(actions[1].payload.response.status).toEqual(404);
expect(actions[1].payload.message).toEqual('BAD_REQUEST');
done();
});

});Action creator// tipos de acciones que se generaran
export const types = {
SETPASS_STARTED: '#password/setPass_started',
SETED_PASS: '#password/seted_pass',
LEAVE_COMPONENT: '#password/leave_pass',
RESTORE_STARTED: '#password/restore_started',
RESTORE_PASS: '#password/restore',
};

// creador de accion cuando se setea el password por primera vez
export const password = (values) => {
const data = {
newPassword: values.password,
email: values.email,
key: values.key,
};
let request = axios.post(${Config.baseUrl}${REGISTER_ENDPOINT}, data, {
auth: {
username: Config.oauth.appUser,
password: Config.oauth.appPassword,
},
});

return [{
type: types.SETPASS_STARTED,
}, {
type: types.SETED_PASS,
payload: request,
}];
};`

@canoqb10 ,

The default time moxios waits before it returns a mock response is 100ms, and you have to override it on each individual call. (There's an open issue about this being too long, #16 )

You can add a small number at the end of the parameter list for each moxios method call to reduce this delay.

E.g.,

moxios.stubRequest(path, {
  status: 404,
  response: {
    message:'BAD_REQUEST'
  }
}, 5);

where that last 5 changes the delay to 5ms.

Hope this helps.

Kamino cloned this issue to anilanar/moxios

I had the same issue, this is the first result when googling this issue. So I leave here my solution.
If you have multiple moxios like this

moxios.wait(() => {
  const requestInternet = moxios.requests.at(0);
  requestInternet.respondWith({
    status: 200,
    response: true
  });
  done(); //add the done() callback after the first request to know what's really happening
  moxios.wait(() => {
    const requestTransaction = moxios.requests.at(1);
    requestTransaction.respondWith({
      status: 200,
      response: transactionResult
    });
    done();
  });
});

It won't be able to show you the actual error because it fails on the first call and it's waiting for the done() callback to end. so if you add a temporary done() after the first call, you will see what actually fails... in my case my action has a json stringified as a parameter but i gave a actual json in my test, so it was failing on JSON.parse(object),

I'm trying to use moxios.wait within another moxios.wait and I'm getting

Error: done() called multiple times