Jest URL.createObjectURL is not a function

前端 未结 4 1499
[愿得一人]
[愿得一人] 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:06

    This would appear to be as simple as setting up URL on the Global in Jest. Something like

    describe('download', () => {
      const documentIntial = { content: 'aaa' };
      global.URL.createObjectURL = jest.fn();
      it('msSaveOrOpenBlob should not have been called when navigao is undefined', () => {
        global.URL.createObjectURL = jest.fn(() => 'details');
    window.navigator.msSaveOrOpenBlob = jest.fn(() => 'details');
    download(documentIntial);
    expect(window.navigator.msSaveOrOpenBlob).toHaveBeenCalledTimes(1);
      });
    });
    

    This should result in a test that you can also use for checking if global.URL.createObjectURL was called. As a side note: you may also run into a similar issue with window.open I would suggest mocking that as well if this becomes the case.

提交回复
热议问题