简体   繁体   English

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

[英]How to properly require modules from mocha.opts file

I'm using the expect.js library with my mocha unit tests. 我正在使用expect.js库和我的mocha单元测试。 Currently, I'm requiring the library on the first line of each file, like this: 目前,我要求每个文件的第一行上的库,如下所示:

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 . 如果可能的话,我想从每个文件的第一行删除样板需求代码 ,让我的单元测试神奇地了解expect I thought I might be able to do this using the mocha.opts file: 我以为我可以使用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 ReferenceError:未定义expect

This seems to make sense - how can it know that the reference to expect in 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: 期望库肯定会被加载,好像我将路径更改为不存在的东西然后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任务中运行我的测试。

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.opts to: 并将您的test/mocha.opts设置为:

--require test/helper

While Louis's answer is spot on, in the end I solved this with a different approach by using karma and the karma-chai plugin: 虽然路易斯的回答很明显,但最后我用一种不同的方法解决了这个问题,使用了业力和业力猜这个插件:

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
    });
});

Thanks to Louis answer and a bit of fiddling around I sorted out my test environment references using mocha.opts. 感谢路易斯的回答和一些摆弄,我使用mocha.opts整理了我的测试环境参考。 Here is the complete setup. 这是完整的设置。

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文件中使用脚本标记和使用require进行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. 所以我摆脱了文件中的require部分,并在我的测试文件夹中使用此内容设置了mocha.opts文件。 The paths are relative to the root folder: 路径相对于根文件夹:

--require test/mocha.opts.js

mocha.opts.js content. mocha.opts.js的内容。 The paths are relative to the location of the file: 路径相对于文件的位置:

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

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

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