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

后端 未结 13 559
轻奢々
轻奢々 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 01:10

    Extending @jabacchetta's solution to avoid this setting bleeding into other tests:

    describe("Example", () => {
      let location;
    
      beforeEach(() => {
        const url = "https://example.com";
        location = window.location;
        const mockLocation = new URL(url);
        mockLocation.replace = jest.fn();
        delete window.location;
        window.location = mockLocation;
      });
    
      afterEach(() => {
        window.location = location;
      });
    });
    
    0 讨论(0)
提交回复
热议问题