【发布时间】:2017-07-01 23:56:10
【问题描述】:
Jest,通过我想象的 JSDom,没有定义 document.createRange。如何覆盖或提供此行为?
我们为自定义 JSDom + mocha 设置(在所有测试之前运行)编写的版本如下所示:
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,
};
};
有没有办法提供这个来开玩笑?
【问题讨论】:
标签: javascript jestjs jsdom