【发布时间】:2021-11-25 17:56:35
【问题描述】:
我正在使用 jest + react 测试库
正如标题所描述的,我要测试的是 testAction 调度了 secondTestAction,但是我收到一个错误,说 secondTestAction 没有调度,这不可能是真的,因为正在打印函数上的消息。
任何人都知道我怎样才能使这个测试工作?
动作
export const testAction = () => async (dispatch) => {
dispatch(secondTestAction());
};
export const secondTestAction = () => {
console.log('second action have been dispatched');
};
测试
import '@testing-library/jest-dom/extend-expect';
import * as userActions from '../../../redux/actions/userActions';
import store from '../../../redux/store';
const secondTestActionSpy = jest.spyOn(userActions, 'secondTestAction');
test('should dispatch secondTestActions ', async () => {
await store.dispatch(userActions.testAction());
expect(secondTestActionSpy.mock.calls.length).toBe(1);
});
错误
expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 0
expect(secondTestActionSpy.mock.calls.length).toBe(1);
【问题讨论】:
标签: reactjs redux react-redux jestjs react-testing-library