简体   繁体   English

如何使用Mocha创建自定义报告者

[英]How to create a custom reporter with Mocha

I am sure I am missing something obvious here! 我相信我错过了一些明显的东西!

I have read the instructions here ( https://github.com/visionmedia/mocha/wiki/Third-party-reporters ), and have taken their code and added as a new node module (ie it is within node_modules/my-reporter/reporter.js ). 我已经阅读了这里的说明( https://github.com/visionmedia/mocha/wiki/Third-party-reporters ),并已将其代码添加为新节点模块(即它位于node_modules/my-reporter/reporter.js )。 However, I can't seem to get mocha to load this reporter. 但是,我似乎无法让摩卡加载这位记者。

I have tried numerous variations … 我尝试了很多变化......

mocha allTests.js -R ./node_modules/my-reporter/reporter.js

mocha allTests.js -R my-reporter

But nothing works :-( 但没有任何作用:-(

I can successfully load my reporter within a JS file: 我可以在JS文件中成功加载我的记者:

my_reporter = require('./node_modules/my-reporter/reporter.js')
console.log(my_reporter);

Has anyone got any hints? 有人有任何提示吗?

u should provide the reporter like this: 你应该像这样提供给记者:

mocha allTests.js -R './node_modules/my-reporter/reporter'

You should not provide the .js file extension as that is the normal convention for including modules. 您不应该提供.js文件扩展名,因为这是包含模块的常规约定。

It appears that if mocha is installed globally (which I believe it almost always is), you have to install your reporter the same way. 似乎如果全局安装了mocha(我相信它几乎总是如此),你必须以同样的方式安装你的记者。

If you don't want to publish the reporter as a public module, you can just: 如果您不想将报告者作为公共模块发布,您可以:

npm pack
npm install /path/to/your/module.gz -g

I've tried putting the reporter everywhere else that would make sense, but was getting "invalid reporter" unless it was installed globally. 我已经尝试将记者放在其他任何有意义的地方,但除非是全球安装,否则会得到“无效的记者”。

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

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