我正在使用Jest进行问题测试。我和create-react-app
做了一个反应应用并做了一些测试。现在,我使用模拟数据的所有测试都失败了。好像jest.mock('../../../main/services/api');
似乎没有模仿任何东西,正如你可以看到错误下面的
TypeError: _ScheduleAPI2.default.addAvailability is not a function
at src/schedule/actions/scheduleActions.js:173:7183
at Object.dispatch (node_modules/redux-thunk/lib/index.js:11:18)
at Object.it (src/schedule/actions/__tests__/scheduleActions-test.js:361:18)
at runMicrotasksCallback (internal/process/next_tick.js:64:5)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
测试示例:
import ScheduleAPI from '../../services/ScheduleAPI';
jest.mock('../../services/ScheduleAPI');
.
.
.
it('creates LOAD_MORE_APPROVED_APPOINTMENTS_FAILURE when loading more approved appointments fails', () => {
ScheduleAPI.fetchApprovedAppointments.mockImplementation(() => {
throw { status: 401, error: { error: 'unauthorized' } };
});
return store.dispatch(actions.loadMoreApprovedAppointments(A_DATE))
.catch(() => {
expect(store.getActions()).toEqual([
{ type: LOAD_MORE_APPROVED_APPOINTMENTS_REQUEST },
{ type: LOAD_MORE_APPROVED_APPOINTMENTS_FAILURE },
]);
});
});
最奇怪的是,这个测试通过项目CI和同事。任何人都知道为什么?
答案 0 :(得分:0)
您需要明确throw new Error()
表示错误,否则throw
将.then()
而不是.catch()
。此外,throw new Error('unauthorized')
的参数是一个字符串,用作错误的描述,您无法发送状态或响应对象。