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

编程入门 行业动态 更新时间:2024-10-22 23:49:04
本文介绍了如何正确地要求mocha.opts文件中的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用我的摩卡咖啡单元测试的expect.js库。目前,我在每个文件的第一行都要求使用库,如下所示:

var expect = require('expect的.js'); $ b $ describe('something',function(){ it('should pass',function(){ expect(true).to.be(true); / / works }); });

如果可能,我想从第一行删除样板代码每个文件,并让我的单元测试神奇地知道 expect 。我想我可以使用 mocha.opts 文件来完成此操作:

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

但是现在我运行我的测试时出现以下错误: b $ b

ReferenceError:expect不是定义

这似乎有道理 - 它如何知道对的引用期望在我的测试中引用了expect.js库导出的内容吗?

期望的库肯定会被加载,就像我将路径更改为non-然后摩卡说:

错误:无法找到模块'./does-not-exist.js'

有什么方法可以完成我想要的吗?我正在运行我的测试,如果这可能会有帮助。

想通了,模块导出的符号不会自动进入全局空间。您可以使用您自己的帮助模块对此进行补救。

创建 test / helper.js :

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

并设置您的 test / mocha.opts 到:

- 需要测试/帮助

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:

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.opts file:

--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 expect in my tests refers to what is exported by the expect.js library?

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

"Error: Cannot find module './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.

解决方案

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:

var expect = require("expect.js") global.expect = expect;

and set your test/mocha.opts to:

--require test/helper

更多推荐

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

本文发布于:2023-06-04 08:34:10,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确地   模块   文件   opts   mocha

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!