Umi Jest

Mock History

umi中,history是命令行导航或说是路由的方式。我们可以通过像history.push("/")的方式进行路由跳转。当要给history添加单元测试的使用,我们可以使用下面的方式:

import { history } from 'umi';
jest.mock("umi",()=>{
    return {
        history: {
            push: jest.fn()
        }
    }
})
describe('history', function () {
    it('should be /', function () {
        expect(history.push).toBeCalledWith("/");
    });
});

这里的关键点在于:

  1. 在当前测试中添加history的显示引入
  2. 使用toBeCalledWith来判定history的push方法接收到的是我们预期的参数。