Skip to content

Error : Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. #13

Closed
@howardyclo

Description

@howardyclo

I'm using Jest to do unit test based on Jasmine 2
When I was testing my async action store having Parse query
It throws this error :
''Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVL."

Here is my code from _tests/actions/auth-test.js

it('should creates SIGNUP_REQUEST and SIGNUP_SUCCESS when signup has been done successfully', (done) => {

        /* If this account has already exists in Parse database, it will get SIGNUP_ERROR */

        const expectedActions = [
            { type: 'SIGNUP_REQUEST', payload: { username, password } },
            { type: 'SIGNUP_SUCCESS', payload: { user: { /* whatever. Anyway, it needs to be defined */ } } },
        ];

        const store = mockStore({}, expectedActions, done);

        store.dispatch(Auth.signup(username, password));
    })

Finally, I revised some code from yours and tested successfully.
It seems that the main problem is Jasmine done() doesn't be invoked, so it causes this error.

Here is my revised code :

function configureMockStore(middlewares = []) {

    return function mockStore(getState, expectedActions, done) {

        if (!Array.isArray(expectedActions)) {
            throw new Error('expectedActions should be an array of expected actions.');
        }

        if (typeof done !== 'undefined' && typeof done !== 'function') {
            throw new Error('done should either be undefined or function.');
        }

        function mockStoreWithoutMiddleware() {
            return {
                getState() {
                    return typeof getState === 'function' ? getState() : getState;
                },

                dispatch(action) {

                    const expectedAction = expectedActions.shift();

                    /* Customize for auth action test */
                    switch(action.type) {

                    case 'SIGNUP_REQUEST':
                    case 'LOGIN_REQUEST':
                        expect(action).toEqual(expectedAction);
                        break;
                    case 'SIGNUP_SUCCESS':
                    case 'LOGIN_SUCCESS':
                        expect(action.type).toEqual(expectedAction.type);
                        expect(action.payload.user).toBeDefined();
                        break;
                    case 'SIGNUP_ERROR':
                    case 'LOGIN_ERROR':
                        expect(action.type).toEqual(expectedAction.type);
                        expect(action.payload.error).toBeDefined();
                        break;
                    }

                    if (done && !expectedActions.length) {
                        done();
                    }

                    return action;
                }
            }
        }

        const mockStoreWithMiddleware = applyMiddleware(
            ...middlewares
        )(mockStoreWithoutMiddleware);

        return mockStoreWithMiddleware();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions