Jest URL.createObjectURL is not a function

前端 未结 4 1515
[愿得一人]
[愿得一人] 2021-01-17 07:39

I\'m developping a reactJs application. I\'m using jest to test my application. I want to test a function that download a blob.

But unfortunately I receve this error

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 08:11

    Since window.URL.createObjectURL is not (yet) available in jest-dom, you need to provide a mock implementation for it.

    Don't forget to reset the mock implementation after each test.

    describe("your test suite", () => {
      window.URL.createObjectURL = jest.fn();
    
      afterEach(() => {
        window.URL.createObjectURL.mockReset();
      });
    
      it("your test case", () => {
        expect(true).toBeTruthy();
      });
    });
    

提交回复
热议问题