Javascript 如何在mocha单元测试中模拟document.evaluate()和XPathResult?
我有以下JavaScript类包装Javascript 如何在mocha单元测试中模拟document.evaluate()和XPathResult?,javascript,dom,xpath,mocha.js,jsdom,Javascript,Dom,Xpath,Mocha.js,Jsdom,我有以下JavaScript类包装document.evaluate()使用XPath选择DOM元素: 'use strict' Object.defineProperty(exports, '__esModule', { value: true }) class ElementByXPath { constructor(elementXPath) { this.elementXPath = elementXPath } element(document) { ret
document.evaluate()
使用XPath选择DOM元素:
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
class ElementByXPath {
constructor(elementXPath) {
this.elementXPath = elementXPath
}
element(document) {
return document.evaluate(
this.elementXPath,
document.documentElement,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null,
).singleNodeValue
}
}
exports.ElementByXPath = ElementByXPath
以下用摩卡咖啡编写的测试应验证方法元素()
:
有人给我个暗示吗?谢谢。在
jsdom
旁边添加jsdom global
(已描述):
当mocha
执行测试时,注册jsdomglobal
mocha -r jsdom-global/register test/**/*.test.js
ReferenceError: XPathResult is not defined
npm i -D jsdom jsdom-global
mocha -r jsdom-global/register test/**/*.test.js