我有一个react-redux应用程序,其中希望在发生故障时测试操作的返回值。各个动作创建者的代码如下:
getData: values => async dispatch => {
dispatch(ACTIONS.getData());
const result = await values.map(value => {
const { valueOne } = value;
return fetchApi(`testURL`)
.then(data => {
dispatch(ACTIONS.getDataSuccess(data));
return data;
})
.catch(() => {
dispatch(
ACTIONS.getDataError({
valueOne,
}),
);
});
});
return result;
},
我希望测试此动作创建者,以确保在调用失败时得到“未定义”。这是我这样做的测试代码:
test('getData failure to return [undefined]', async () => {
fetchApi.mockImplementation(() => Promise.reject('ERROR'));
const action = DataActions.getData([values[0]]);
await expectResultsArray(action, [undefined]);
});
export const expectResultsArray = async (action, expectedResult) => {
const store = mockStore();
const result = await store.dispatch(action);
jestExpect(result).toStrictEqual(expectedResult);
};
由于以下原因,该测试在指定的行(带有符号“>”的行)失败:
expect(received).toStrictEqual(expected)
- Expected
+ Received
Array [
- undefined,
+ Promise {},
]
const store = mockStore();
const result = await store.dispatch(action);
> jestExpect(result).toStrictEqual(expectedResult);
};
该代码有效,运行时将为相应的Promise返回“ undefined”。我在测试中做错了什么?目的是确保调用失败时结果为“ [undefined]”。