javascript 以编程方式运行 Mocha 并将结果传递给变量或函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29050720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:01:01  来源:igfitidea点击:

Run Mocha programmatically and pass results to variable or function

javascriptjsonnode.jsmocha

提问by dlearious

I've setup a suite of tests in mocha using ZombieJS and Chai. The tests load up a website and check if various services are booked in correctly and are displaying to visitors of the website.

我已经使用 ZombieJS 和 Chai 在 mocha 中设置了一套测试。测试加载网站并检查是否正确预订了各种服务以及是否向网站访问者显示。

What I'm aiming for is that the tests will run daily and then email the results to my team. The tests are all running as expected but the blockage I've hit is the following.

我的目标是每天运行测试,然后将结果通过电子邮件发送给我的团队。测试都按预期运行,但我遇到的障碍如下。

How do I pass the JSON reporter results to another node.js script where I can email the results. Building the email and sending it is going to be straight forward using nodemailer and underscore templating.

如何将 JSON 报告器结果传递到另一个 node.js 脚本,在那里我可以通过电子邮件发送结果。使用 nodemailer 和下划线模板构建和发送电子邮件将是直接的。

My current thinking is there are two approaches. Run the mocha test with a shell script and pipe the JSON output to a node script and process the JSON from a command line argument. Something like...

我目前的想法是有两种方法。使用 shell 脚本运行 mocha 测试并将 JSON 输出通过管道传输到节点脚本,并从命令行参数处理 JSON。就像是...

mocha test/services/homepage.js > node email.js

The other alternative is to run the tests from within a node script and get the returned result in a variable. I've been using information from here to run the tests within node.

另一种选择是从节点脚本中运行测试并在变量中获取返回的结果。我一直在使用这里的信息在 node.js 中运行测试。

https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically

https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically

This runs correctly but I'm lost with how to get the JSON reporter results into a variable from the below code.

这运行正确,但我不知道如何从以下代码将 JSON 报告结果转换为变量。

var Mocha = require('mocha'),
    Suite = Mocha.Suite,
    Runner = Mocha.Runner,
    Test = Mocha.Test;

// First, you need to instantiate a Mocha instance

var mocha = new Mocha({
    reporter: 'json'
});

var suite = new Suite('JSON suite', 'root');
var runner = new Runner(suite);
var mochaReporter = new mocha._reporter(runner);

mocha.addFile(
    '/Users/dominic/Git/testing-rig/test/services/homepage.js'
);

runner.run(function(failures) {
    // the json reporter gets a testResults JSON object on end
    var testResults = mochaReporter.testResults;

    console.log(testResults);
    // send your email here
});

回答by riaan53

You can listen to the runner events as in https://github.com/mochajs/mocha/blob/master/lib/runner.js#L40and build your own report.

您可以在https://github.com/mochajs/mocha/blob/master/lib/runner.js#L40 中收听跑步者事件并构建您自己的报告。

var Mocha = require('mocha');

var mocha = new Mocha({});

mocha.addFile('/Users/dominic/Git/testing-rig/test/services/homepage.js')

mocha.run()
    .on('test', function(test) {
        console.log('Test started: '+test.title);
    })
    .on('test end', function(test) {
        console.log('Test done: '+test.title);
    })
    .on('pass', function(test) {
        console.log('Test passed');
        console.log(test);
    })
    .on('fail', function(test, err) {
        console.log('Test fail');
        console.log(test);
        console.log(err);
    })
    .on('end', function() {
        console.log('All done');
    });

回答by Ricky

I'd suggest using a mocha reporter as explained here https://github.com/mochajs/mocha/wiki/Third-party-reporters

我建议使用 mocha 记者,如此处所述 https://github.com/mochajs/mocha/wiki/Third-party-reporters

invoke mocha like this

像这样调用摩卡咖啡

MyReporter = require('./MyReporter');
mocha({ reporter: MyReporter })`

and the MyReporter.jsfile will look like this

MyReporter.js文件将如下所示

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

function MyReporter(runner) {
  mocha.reporters.Base.call(this, runner);
  var passes = 0;
  var failures = 0;

  runner.on('pass', function(test){
    passes++;
    console.log('pass: %s', test.fullTitle());
  });

  runner.on('fail', function(test, err){
    failures++;
    console.log('fail: %s -- error: %s', test.fullTitle(), err.message);
  });

  runner.on('end', function(){
    console.log('end: %d/%d', passes, passes + failures);
    process.exit(failures);
  });
}

回答by Lim H.

Hmm normally people would use a CI bot to achieve what you are trying to do. However, regarding your direct question about getting the result from JSON reporter, I don't know if there is a better way to achieve it, but here is what I'd do after reading the mocha source. You'll have to create the Suite, the Runner and get the reporter yourself (copy from https://github.com/mochajs/mocha/blob/master/test%2Freporters%2Fjson.js):

嗯,通常人们会使用 CI bot 来实现您想要做的事情。但是,关于您关于从 JSON 报告器获取结果的直接问题,我不知道是否有更好的方法来实现它,但这是我在阅读 mocha 源代码后会做的事情。您必须创建套件、跑步者并自己获取记者(从https://github.com/mochajs/mocha/blob/master/test%2Freporters%2Fjson.js复制):

var mocha = new Mocha({
    reporter: 'json'
});
var suite = new Suite('JSON suite', 'root');
var runner = new Runner(suite);
var mochaReporter = new mocha._reporter(runner);

runner.run(function(failures) {
    // the json reporter gets a testResults JSON object on end
    var testResults = mochaReporter.testResults;
    // send your email here
});