2
Jest,通過JSDom我想,沒有document.createRange
定義。我如何覆蓋或提供這種行爲?Mocking document.createRange for jest
我們爲我們的定製JSDom +摩卡設置寫的版本(所有測試跑前)看起來是這樣的:
global.Range = function Range() {};
const createContextualFragment = (html) => {
const div = document.createElement('div');
div.innerHTML = html;
return div.children[0]; // so hokey it's not even funny
};
Range.prototype.createContextualFragment = (html) => createContextualFragment(html);
// HACK: Polyfil that allows codemirror to render in a JSDOM env.
global.window.document.createRange = function createRange() {
return {
setEnd:() => {},
setStart:() => {},
getBoundingClientRect:() => {
return { right: 0 };
},
getClientRects:() => [],
createContextualFragment,
};
};
有沒有提供這開玩笑的方式?
效果很好,謝謝! – scniro