javascript Jest URL.createObjectURL 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52968969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jest URL.createObjectURL is not a function
提问by Melchia
I'm developping a reactJs application. I'm using jest to test my application. I want to test a function that download a blob.
我正在开发一个 reactJs 应用程序。我正在使用 jest 来测试我的应用程序。我想测试一个下载 blob 的函数。
But unfortunately I receve this error:
但不幸的是我收到了这个错误:
URL.createObjectURL is not a function
URL.createObjectURL 不是函数
my test function:
我的测试功能:
describe('download', () => {
const documentIntial = { content: 'aaa' };
it('msSaveOrOpenBlob should not have been called when navigao is undefined', () => {
window.navigator.msSaveOrOpenBlob = null;
download(documentIntial);
expect(window.navigator.msSaveOrOpenBlob).toHaveBeenCalledTimes(0);
});
});
The function I want to test:
我要测试的功能:
export const download = document => {
const blob = new Blob([base64ToArrayBuffer(document.content)], {
type: 'application/pdf',
});
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob);
return;
}
const fileURL = URL.createObjectURL(blob);
window.open(fileURL);
};
回答by Moosecouture
This would appear to be as simple as setting up URLon the Global in Jest. Something like
这看起来就像URL在 Jest 中设置Global一样简单。就像是
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.createObjectURLwas called. As a side note: you may also run into a similar issue with window.openI would suggest mocking that as well if this becomes the case.
这应该会产生一个测试,你也可以用它来检查是否global.URL.createObjectURL被调用。附带说明:您也可能会遇到类似的问题,window.open如果情况如此,我建议您也嘲笑一下。
回答by Kaiido
jsdom, the JavaScript implementation of the WHATWG DOM used by jest doesn't implement this method yet.
在 jsdom 中,jest 使用的 WHATWG DOM 的 JavaScript 实现尚未实现此方法。
You can find an open ticket about this exact issueon their github pagewhere some workarounds are provided in comments. But if you need the blobURL to actually work you'll have to wait this FR is solved.
您可以在他们的github 页面上找到有关此确切问题的公开票证,其中评论中提供了一些解决方法。但是,如果您需要 blobURL 实际工作,则必须等待此 FR 解决。
Workaround proposed in the comments of the issue for jest:
开玩笑的问题评论中提出的解决方法:
function noOp () { }
if (typeof window.URL.createObjectURL === 'undefined') {
Object.defineProperty(window.URL, 'createObjectURL', { value: noOp})
}
回答by Hymandbd
Since window.URL.createObjectURLis not (yet) available in jest-dom, you need to provide a mock implementation for it.
由于window.URL.createObjectURL(尚未)在 jest-dom 中可用,您需要为它提供一个模拟实现。
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();
});
});

