【发布时间】:2020-01-08 15:36:32
【问题描述】:
下午好,我开始处理 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);
});
});
【问题讨论】:
标签: javascript unit-testing mocha.js jsdom