简体   繁体   English

如何与记者一起运行摩卡测试?

[英]How to run mocha tests with reporter?

When I run npm test it outputs:当我运行npm test它输出:

 mocha ./tests/ --recursive --reporter mocha-junit-reporter

And all tests run well.并且所有测试运行良好。 But when I try to invoke mocha ./tests/flickr/mytest --reporter junit-reporter I got:但是当我尝试调用 mocha ./tests/flickr/mytest --reporter junit-reporter我得到:

 Unknown "reporter": junit-reporter

How to pass it correctly?如何正确传递?

From mocha-junit-reporter 's readme :来自mocha-junit-reporter自述文件

# install the package
npm install mocha-junit-reporter --save-dev

# run the test with reporter
mocha test --reporter mocha-junit-reporter --reporter-options mochaFile=./path_to_your/file.xml

I spotted two issues in your command:我在您的命令中发现了两个问题:

mocha ./tests/flickr/mytest --reporter junit-reporter

First issue is mocha in above is a mocha command from global node module.第一个问题是mocha上面是全球节点模块摩卡命令。 However, when executing npm test , it actually targets local mocha command inside our node_modules folder.但是,当执行npm test ,它实际上是针对node_modules文件夹中的本地mocha命令。

Second issue is the name of reporter should be mocha-junit-reporter not junit-reporter第二个问题是记者的名字应该是mocha-junit-reporter而不是junit-reporter

Solution解决方案

Workaround is to target local mocha解决方法是针对本地mocha

./node_modules/.bin/mocha ./tests/flickr/mytest --reporter mocha-junit-reporter

This is preferrable solution.这是优选的解决方案。

Alternative solution is to install mocha-junit-reporter for global node modules as below:替代解决方案是为全局节点模块安装mocha-junit-reporter ,如下所示:

npm install -g mocha-junit-reporter
mocha ./tests/flickr/mytest --reporter mocha-junit-reporter

This is not too preferrable because you can target different version of mocha and mocha-junit-reporter in global compare to the one in local node modules.这不是太可取,因为与本地节点模块中的版本相比,您可以在全局中定位不同版本的 mocha 和 mocha-junit-reporter。

Cheers干杯

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

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