node.js mocha 命令给出 ReferenceError: 窗口未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34059644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:36:25  来源:igfitidea点击:

mocha command giving ReferenceError: window is not defined

node.jsmochajsdom

提问by jit

I am using command:

我正在使用命令:

mocha --compilers :./test/babel-setup.js --recursive --watch

It is giving error:

它给出了错误:

ReferenceError: window is not defined

I have the following in my babel-setup.js:

我的 babel-setup.js 中有以下内容:

require("babel/register")({
  compact: false
});

I am using node 0.12.7 and "mocha": "^2.2.5" and "jsdom": "^3.1.2".

我正在使用节点 0.12.7 和“mocha”:“^2.2.5”和“jsdom”:“^3.1.2”。

I already have jsdominstalled with the project.

我已经在项目中安装了jsdom

What is causing the error?

导致错误的原因是什么?

回答by emmby

I was able to use jsdom-globalto fix this issue. Follow the instructions in that link to install. Specifically, run

我能够用来jsdom-global解决这个问题。按照该链接中的说明进行安装。具体来说,运行

npm install --save-dev --save-exact jsdom jsdom-global

then add -r jsdom-global/registerto your mocha command-line. When you re-run your tests, the window is not definederror will go away.

然后添加-r jsdom-global/register到您的 mocha 命令行。当您重新运行测试时,window is not defined错误将消失。

回答by Henry31

To test frontend libraries with mocha you need the node modul mocha-jsdomyou find it here: https://www.npmjs.com/package/mocha-jsdom

要使用 mocha 测试前端库,您需要在此处找到节点模块mocha-jsdomhttps: //www.npmjs.com/package/mocha-jsdom