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