How to mock useHistory hook in jest?

前端 未结 5 542
無奈伤痛
無奈伤痛 2020-12-17 07:55

I am using UseHistory hook in react router v5.1.2 with typescript? When running unit test, I have got issue.

TypeError: Cannot read property \'history

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 08:39

    In github react-router repo i found that useHistory hook uses singleton context, when i started use in mount MemoryRouter it found context and started works. So fix it

    import { MemoryRouter } from 'react-router-dom';
    const tree =  mount( );
    

提交回复
热议问题