[下午好,我开始处理jsdom。为了清晰起见,编写了一个非常简单的功能。使用mocha,chai和JSDOM编写了一个测试。
module.js
中的所有“文档”都被隐藏,因为如果我取消注释,则会出现错误"ReferenceError: document is not defined"
。从理论上讲,我已经理解了它的含义。此错误意味着在Node中没有窗口,文档等。但是我不知道如何解决这个问题。
我将不胜感激。
index.html:
<body>
<input type="text" id="inputValue">
<input type="text" id="outputValue">
<button id="getResult">getResult</button>
<script src="./module.js"></script>
</body>
module.js
//var input = document.getElementById("inputValue");
//var output = document.getElementById("outputValue");
//var button = document.getElementById("getResult").addEventListener('click', generateValue);
function generateValue(input, output){
output.value = input.value * 5;
}
module.exports = {
generateValue,
};
tests.js
let object = require("../module.js");
const assert = require("chai").assert;
const { JSDOM } = require("jsdom");
describe("generateValue", function() {
let dom = null;
before(async function() {
dom = new JSDOM('<!doctype html><html><head></head><body></body></html>')
});
it('Should return 25 when parameter 5', function() {
const input = dom.window.document.createElement("input");
input.id = "inputValue";
const output = dom.window.document.createElement("input");
output.id = "outputValue";
dom.window.document.body.append(input);
dom.window.document.body.append(output);
input.value = 5;
const expected = 25;
object.generateValue(input, output);
assert.equal(output.value, expected);
});
});