简体   繁体   English

挂钩自定义摩卡记者

[英]Hooking a custom mocha reporter

I am new to javascript and mocha. 我是javascript和mocha的新手。 I have been looking at how to create a third party reporter. 我一直在研究如何创建第三方记者。 I saw some samples at https://github.com/mochajs/mocha/wiki/Third-party-reporters 我在https://github.com/mochajs/mocha/wiki/Third-party-reporters上看到了一些示例

I created one that meets my needs and was able to install it and use it. 我创建了一个可以满足自己需求的软件,并且能够安装和使用它。 But, the requirement is to not install the reporter. 但是,要求是不安装报告器。 It can either be a different file or be part of the same js file. 它可以是其他文件,也可以是同一js文件的一部分。

Can anyone please tell me how to hook the reporter with the js file? 谁能告诉我如何用js文件钩住记者?

Here is my test js file 这是我的测试js文件

const mochasteps = require('mocha-steps')
var Mocha = require('mocha');
var report = require('./report')
var mochainstance = new Mocha({reporter: report});
console.log(mochainstance._reporter)

before(() => console.log('before'))
after(() => console.log('after'))

describe('Array', () => {
    step('should start empty', done => {
    var arr = [];
    if (arr.length == 1) return done()
        done(new Error('array length not 0'))
  });
});

describe('Test', () => {
    step('some Test', done => {
    done(); 
  });
});

Here is my test report.js file that does the reporting. 这是我执行报告的测试report.js文件。

var mocha = require('mocha');
module.exports = report;

function report(runner) {
  mocha.reporters.Base.call(this, runner);

  runner.on('pass', function(test){
    console.log('[pass]%s', test.title);
  });

  runner.on('fail', function(test, err){
    console.log('[fail]%s(%s)', test.title, err.message);
  });

  runner.on('end', function(){
    process.exit(0);
  });
}

Thanks, r1j1m1n1 谢谢,r1j1m1n1

You need to pass the path to the custom reporter when running the tests. 运行测试时,您需要将路径传递给自定义报告程序。 In your example, assuming test.js and report.js are in the same folder, it would be as follows: 在您的示例中,假设test.js和report.js位于同一文件夹中,则如下所示:

mocha test.js -R 'report.js'

This will run the test.js tests with report.js as the reporter without having to install the reporter. 这将以Report.js作为报告程序运行test.js测试,而无需安装报告程序。

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

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