vendredi 21 septembre 2018

Testing typescript asyc functions, Async callback was not invoked even though it's called in .then() method

I've been searching around for a while for a resolution to this but can't find anything, the only thing I can think of is redux-mock-store doesn't support Promises.

I have this test method below.

import thunk from 'redux-thunk';
import configureMockStore from 'redux-mock-store';
import { cleanAll } from 'nock';

var middleware = [thunk];
var mockStore = configureMockStore(middleware);

describe('Organisation thunk actions', () => {
    afterAll(() => {
        cleanAll();
    });

    describe('getOrganisation', () => {
        test('should create BEGIN_GET_ORGANISATION_AJAX action when getting organsation', (done: any) => {
            jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
            var expectedAction = [{ type: types.BEGIN_GET_ORGANISATION_AJAX }];
            var store = mockStore({});
            store.dispatch<any>((OrganisationActions.getOrganisation("e"))).then(() => {
                var actions = store.getActions();
                expect(actions).toEqual(expectedAction);
                done();
            });
        });
    });
});

This is designed to test the action below.

export const getOrganisation = (organisationId: string, expands?: string[]) => {
    return (dispatch: any) => {
        dispatch(beginGetOrganisationAjax());
            return OrganisationApi.getOrganisationAsync(organisationId).then((organisation: any) =>     {
            dispatch(getOrganisationSuccess(organisation))
        }).catch(error => {
            throw (error);
        });
    }
}

Where OrganisationApi.getOrganisationAsync(organisationId) is a call to a mockApi that I know works from usage.

When I run this test it fails twice after the 30s DEFAULT_TIMEOUT_INTERVAL specified, once as expected (as I've designed the expect to) but the second failure is an error "Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.".

Unless I'm mistaken, the async callback is the done() function invoked after the expect(actions).toEqual(expectedAction) within the .then() function in the test.

As the expect fails the .then() is definitely running but doesn't seem to be running the .done() function, can anyone see why this might be happening?

Aucun commentaire:

Enregistrer un commentaire