Javascript 测试异步Redux操作Jest
我无法从异步redux操作中获得正确的输出。我使用Jest、redux模拟适配器和thunk作为工具 根据redux关于测试async thunks()的文档,我的测试应该返回一个包含两个操作的数组。然而,我的测试只是返回第一个操作,而不是在成功获取时应该返回的第二个操作。我想我只是错过了一些小的东西,但至少要说一点,这是很麻烦的 重复操作Javascript 测试异步Redux操作Jest,javascript,unit-testing,redux,jestjs,redux-mock-store,Javascript,Unit Testing,Redux,Jestjs,Redux Mock Store,我无法从异步redux操作中获得正确的输出。我使用Jest、redux模拟适配器和thunk作为工具 根据redux关于测试async thunks()的文档,我的测试应该返回一个包含两个操作的数组。然而,我的测试只是返回第一个操作,而不是在成功获取时应该返回的第二个操作。我想我只是错过了一些小的东西,但至少要说一点,这是很麻烦的 重复操作 export const getRemoveFileMetrics = cacheKey => dispatch => { dispat
export const getRemoveFileMetrics = cacheKey => dispatch => {
dispatch({ type: IS_FETCHING_DELETE_METRICS });
return axios
.get("GetRemoveFileMetrics", { params: { cacheKey } })
.then(response => dispatch({ type: GET_REMOVE_FILE_METRICS, payload: response.data }))
.catch(err => err);
};
测试
it("getRemoveFileMetrics() should dispatch GET_REMOVE_FILE_METRICS on successful fetch", () => {
const store = mockStore({});
const cacheKey = "abc123doremi";
const removeFileMetrics = {
cacheKey,
duplicateFileCount: 3,
uniqueFileCount: 12,
};
const expectedActions = [
{
type: MOA.IS_FETCHING_DELETE_METRICS,
},
{
type: MOA.GET_REMOVE_FILE_METRICS,
payload: removeFileMetrics,
}
];
mockRequest.onGet(`/GetRemoveFileMetrics?cacheKey=${cacheKey}`).reply(200, removeFileMetrics);
return store.dispatch(MOA.getRemoveFileMetrics(cacheKey)).then(() => {
const returnedActions = store.getActions();
expect(returnedActions).toEqual(expectedActions);
});
});
Expected value to equal:
[{ "type": "IS_FETCHING_DELETE_METRICS" }, { "payload": { "cacheKey": "abc123doremi", "duplicateFileCount": 3, "uniqueFileCount": 12 }, "type": "GET_REMOVE_FILE_METRICS" }]
Received:
[{ "type": "IS_FETCHING_DELETE_METRICS" }]
输出
it("getRemoveFileMetrics() should dispatch GET_REMOVE_FILE_METRICS on successful fetch", () => {
const store = mockStore({});
const cacheKey = "abc123doremi";
const removeFileMetrics = {
cacheKey,
duplicateFileCount: 3,
uniqueFileCount: 12,
};
const expectedActions = [
{
type: MOA.IS_FETCHING_DELETE_METRICS,
},
{
type: MOA.GET_REMOVE_FILE_METRICS,
payload: removeFileMetrics,
}
];
mockRequest.onGet(`/GetRemoveFileMetrics?cacheKey=${cacheKey}`).reply(200, removeFileMetrics);
return store.dispatch(MOA.getRemoveFileMetrics(cacheKey)).then(() => {
const returnedActions = store.getActions();
expect(returnedActions).toEqual(expectedActions);
});
});
Expected value to equal:
[{ "type": "IS_FETCHING_DELETE_METRICS" }, { "payload": { "cacheKey": "abc123doremi", "duplicateFileCount": 3, "uniqueFileCount": 12 }, "type": "GET_REMOVE_FILE_METRICS" }]
Received:
[{ "type": "IS_FETCHING_DELETE_METRICS" }]
我正在使用
jest-fetch-mock
和noaxios
。以下是我的工作与行动。作为第一步,您可以重构为async wait
。对我来说,只有这样才有效
我现在正试图找出如何测试副作用(showErrorAlert(jsonResponse);
)。如果我在测试文件的顶部模拟了bathroralert
实现(在我的示例中注释掉),那么我会遇到与您相同的问题。由于某些原因,无法触发使用fetch的操作
export const submitTeammateInvitation = (data) => {
const config = {
//.....
};
return async (dispatch) => {
dispatch(submitTeammateInvitationRequest());
try {
const response = await fetch(inviteTeammateEndpoint, config);
const jsonResponse = await response.json();
if (!response.ok) {
showErrorAlert(jsonResponse);
dispatch(submitTeammateInvitationError(jsonResponse));
throw new Error(response.statusText);
}
dispatch(submitTeammateInvitationSuccess());
} catch (error) {
if (process.env.NODE_ENV === 'development') {
console.log('Request failed', error);
}
}
};
};
试验