javascript 如何从 mocha.opts 文件中正确地要求模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31282839/
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-10-28 13:35:07  来源:igfitidea点击:

How to properly require modules from mocha.opts file

javascriptnode.jsgulpmocharequire

提问by Mike Chamberlain

I'm using the expect.js library with my mocha unit tests. Currently, I'm requiring the library on the first line of each file, like this:

我在 mocha 单元测试中使用了 expect.js 库。目前,我需要在每个文件的第一行使用该库,如下所示:

var expect = require('expect.js');

describe('something', function () {
    it('should pass', function () {
        expect(true).to.be(true); // works
    });
});

If possible, I'd like to remove the boilerplate require code from the first line of each file, and have my unit tests magically know about expect. I thought I might be able to do this using the mocha.optsfile:

如果可能,我想从每个文件的第一行中删除样板 require 代码,并让我的单元测试神奇地知道expect. 我想我可以使用mocha.opts文件做到这一点:

--require ./node_modules/expect.js/index.js

But now I get the following error when running my test:

但是现在我在运行测试时收到以下错误:

ReferenceError: expect is not defined

参考错误:未定义期望

This seems to make sense - how can it know that the reference to expectin my tests refers to what is exported by the expect.js library?

这似乎是有道理的——它怎么知道expect我测试中的引用是指 expect.js 库导出的内容?

The expect library is definitely getting loaded, as if I change the path to something non-existent then mocha says:

expect 库肯定正在加载,好像我将路径更改为不存在的东西,然后 mocha 说:

"Error: Cannot find module './does-not-exist.js'"

“错误:找不到模块'./does-not-exist.js'”

Is there any way to accomplish what I want? I'm running my tests from a gulp task if perhaps that could help.

有什么办法可以实现我想要的吗?如果可能会有所帮助,我正在从 gulp 任务中运行我的测试。

回答by Louis

You are requiring the module properly but as you figured out, the symbols that the module export won't automatically find themselves into the global space. You can remedy this with your own helper module.

您正确地需要模块,但正如您发现的那样,模块导出的符号不会自动进入全局空间。您可以使用自己的帮助程序模块来解决此问题。

Create test/helper.js:

创建test/helper.js

var expect = require("expect.js")

global.expect = expect;

and set your test/mocha.optsto:

并将您的设置test/mocha.opts为:

--require test/helper

回答by Mike Chamberlain

While Louis's answer is spot on, in the end I solved this with a different approach by using karma and the karma-chaiplugin:

虽然路易斯的回答是正确的,但最后我通过使用 karma 和karma-chai插件用不同的方法解决了这个问题:

Install:

安装:

npm install karma-chai --save-dev

Configure:

配置:

karma.set({

    frameworks: ['mocha', 'chai']
    // ... 
});

Use:

利用:

describe('something', function () {
    it('should pass', function () {
        expect(true).to.be(true); // works
    });
});

回答by pasx

Thanks to Louis answer and a bit of fiddling around I sorted out my test environment references using mocha.opts. Here is the complete setup.

感谢路易斯的回答和一些摆弄,我使用 mocha.opts 整理了我的测试环境参考。这是完整的设置。

My project is a legacy JavaScript application with a lot of "plain" js files which I wish to reference both in an html file using script tags and using require for unit testing with mocha.

我的项目是一个遗留的 JavaScript 应用程序,其中包含许多“普通”js 文件,我希望在使用脚本标记的 html 文件中引用这些文件,并使用需要使用 mocha 进行单元测试。

I am not certain that this is good practice but I am used to Mocha for unit testing in node project and was eager to use the same tool with minimal adaptation.

我不确定这是否是好的做法,但我习惯于在节点项目中使用 Mocha 进行单元测试,并且渴望使用相同的工具进行最小的调整。

I found that exporting is easy:

我发现导出很容易:

class Foo{...}
class Bar{...}
if (typeof module !== 'undefined') module.exports = { Foo, Bar };

or

或者

class Buzz{...}
if (typeof module !== 'undefined') module.exports = Buzz;

However, trying to use require in all the files was an issue as the browser would complain about variables being already declared even when enclosed in an if block such as:

但是,尝试在所有文件中使用 require 是一个问题,因为即使包含在 if 块中,浏览器也会抱怨变量已经声明,例如:

if (typeof require !== 'undefined') {
    var {Foo,Bar} = require('./foobar.js');    
}

So I got rid of the require part in the files and set up a mocha.opts file in my test folder with this content. The paths are relative to the root folder:

所以我去掉了文件中的 require 部分,并在我的测试文件夹中设置了一个包含此内容的 mocha.opts 文件。路径相对于根文件夹:

--require test/mocha.opts.js

mocha.opts.js content. The paths are relative to the location of the file:

mocha.opts.js 内容。路径是相对于文件位置的:

global.assert = require('assert');
global.Foo = require("../foobar.js").Foo;
global.Bar = require("../foobar.js").Bar;
global.Buzz = require("../buzz.js");