简体   繁体   English

“参考错误:文档未定义”

[英]"ReferenceError: document is not defined"

Good afternoon, I started to deal with jsdom.下午好,我开始处理jsdom。 Wrote a very simple function for clarity.为了清楚起见,写了一个非常简单的函数。 Wrote a test using mocha, chai, and JSDOM.使用 mocha、chai 和 JSDOM 编写了一个测试。

All the "documents" in my module.js is hidden due to the fact that if I uncomment them, the error "ReferenceError: document is not defined" will appear.我的module.js所有“文档” module.js被隐藏了,因为如果我取消注释它们,将会出现错误"ReferenceError: document is not defined" In theory, I already understand what it means.理论上,我已经明白这意味着什么。 This error means that in Node there is no window, document, etc .此错误意味着在 Node 中没有窗口、文档等 But I don't know how to solve this problem.但我不知道如何解决这个问题。

I would be grateful for any help.我将不胜感激任何帮助。

在此处输入图片说明

在此处输入图片说明

index.html:索引.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模块.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测试.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);
    });
});

First: You cannot export or import outside a module第一:您不能在模块外导出或导入
this means the node cannot use in html unless you declare to the browser like this: script tag =>这意味着节点不能在 html 中使用,除非你像这样向浏览器声明:脚本标签 =>
node run in the console (Command prompt like c++) and html run in the browser... node 在控制台中运行(像 c++ 一样的命令提示符)和 html 在浏览器中运行...

Second:第二:
Why you want to generateValue and export that you can do this in your test.js file为什么要生成值并导出您可以在 test.js 文件中执行此操作
If you want to do this you must use node as a server like PHP.. (with a little difference)如果你想这样做,你必须像 PHP 一样使用 node 作为服务器..(有一点区别)

by the way you can calculate generate value with another js file and then show that (or every thing you want) and use async function to get value and do something..... but you have to add another script tag in html file顺便说一句,您可以使用另一个 js 文件计算生成值,然后显示该值(或您想要的所有内容)并使用异步函数获取值并执行某些操作.....但是您必须在 html 文件中添加另一个脚本标记

Remember You Cannot Use Node Like JS File in HTML That's why We don't have document and window in node.js记住你不能像 HTML 中的 JS 文件一样使用 Node 这就是为什么我们在 node.js 中没有文档和窗口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM