How to mock [removed].href with Jest + Vuejs?

后端 未结 13 762
轻奢々
轻奢々 2020-12-24 00:32

Currently, I am implementing unit test for my project and there is a file that contained window.location.href.

I want to mock this to test and here is m

13条回答
  •  长情又很酷
    2020-12-24 00:45

    This is valid for Jest + TypeScript + Next.js (in case you use useRoute().push

    const oldWindowLocation = window.location;
    
    beforeAll(() => {
      delete window.location;
      window.location = { ...oldWindowLocation, assign: jest.fn() };
    });
    
    afterAll(() => {
      window.location = oldWindowLocation;
    });
    

提交回复
热议问题