[英]How do I add chaijs's assert in mocha's mocha.opts file?
I don't know if this is even possible. 我不知道这是否有可能。 I know if we just use should.js library we can do the following in
mocha.opts
file 我知道如果只使用should.js库,我们可以在
mocha.opts
文件中执行以下操作
--require should
But since chai
is a parent module and assert
is sub-module of chai
, how do I require it in mocha.opts
file? 但是由于
chai
是chai
的父模块,而assert
是chai
子模块,我如何在mocha.opts
文件中要求它?
Or am I missing something really basic? 还是我真的缺少一些基本知识?
AFAIK chai and mocha are not compatible in this way. AFAIK柴和摩卡咖啡不以这种方式兼容。 It only works with
should
because should
modifies the prototypes of the built-in objects, which some folks find acceptable for this particular use case but in general it is regarded as a bad practice to be avoided. 它只能与
should
因为should
修改内置对象的原型,有些人认为对于特定的用例是可以接受的,但总的来说,应避免将其视为不好的做法。
What you can do instead is write your own little wrapper module that does this: 您可以做的是编写您自己的小包装器模块来执行此操作:
global.assert = require('chai').assert;
And then in your mocha.opts
you can do --require global-chai-assert
and your tests will have access to chai's assert
function as a global. 然后可以在
mocha.opts
中执行--require global-chai-assert
,您的测试将可以使用chai的assert
函数作为全局函数。 I personally think this is an antipattern, but if you like it, that's how I'd do it. 我个人认为这是一种反模式,但是如果您喜欢它,那就是我会这样做的方式。
Since chai v4.0.0 you can register assert/expect/should globals like this: 从chai v4.0.0开始,您可以像这样注册assert / expect / should全局变量:
require('chai/register-assert'); // Using Assert style
require('chai/register-expect'); // Using Expect style
require('chai/register-should'); // Using Should style
So you can add it to your mocha.opts: 因此,您可以将其添加到您的mocha.opts中:
--require chai/register-should
There are some usage examples on the chai's repo README . 柴的回购自述文件中有一些用法示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.