测试打字稿异步函数,即使在 .then() 方法中调用异步回调也未调用
Testing typescript asyc functions, Async callback was not invoked even though it's called in .then() method
我一直在四处寻找解决方案,但找不到任何东西,我唯一能想到的是 redux-mock-store 不支持 Promises。
下面有这个测试方法
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();
});
});
});
});
这是为了测试下面的动作。
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);
});
}
}
其中 OrganisationApi.getOrganisationAsync(organisationId)
是对我从使用中知道有效的 mockApi 的调用。
当我 运行 这个测试在指定的 30 秒后失败了两次 DEFAULT_TIMEOUT_INTERVAL,一次是预期的(正如我设计的那样),但第二次失败是一个错误 "Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.".
除非我记错了,否则异步回调是测试中 .then()
函数中 expect(actions).toEqual(expectedAction)
之后调用的 done()
函数。
由于预期失败,.then()
肯定是 运行ning 但似乎不是 运行ning .done()
函数,任何人都可以看到为什么这可能正在发生?
终于解决了这个问题,贴出解决方案以防有人遇到类似问题。
Async 回调不会被调用,因为测试函数在遇到失败的 expect()
语句时会立即退出,因此在这个实例中它从未实际运行 done()
函数。
此测试通过后,它会调用 done()
函数,我的测试也通过了。
我一直在四处寻找解决方案,但找不到任何东西,我唯一能想到的是 redux-mock-store 不支持 Promises。
下面有这个测试方法
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();
});
});
});
});
这是为了测试下面的动作。
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);
});
}
}
其中 OrganisationApi.getOrganisationAsync(organisationId)
是对我从使用中知道有效的 mockApi 的调用。
当我 运行 这个测试在指定的 30 秒后失败了两次 DEFAULT_TIMEOUT_INTERVAL,一次是预期的(正如我设计的那样),但第二次失败是一个错误 "Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.".
除非我记错了,否则异步回调是测试中 .then()
函数中 expect(actions).toEqual(expectedAction)
之后调用的 done()
函数。
由于预期失败,.then()
肯定是 运行ning 但似乎不是 运行ning .done()
函数,任何人都可以看到为什么这可能正在发生?
终于解决了这个问题,贴出解决方案以防有人遇到类似问题。
Async 回调不会被调用,因为测试函数在遇到失败的 expect()
语句时会立即退出,因此在这个实例中它从未实际运行 done()
函数。
此测试通过后,它会调用 done()
函数,我的测试也通过了。