【问题标题】:Is there a way to get current Mocha instance and edit options at runtime?有没有办法在运行时获取当前的 Mocha 实例和编辑选项?
【发布时间】:2018-05-25 03:22:55
【问题描述】:

假设你有一个简单的摩卡测试:

describe("Suite", function(){
    it("test",function(doneCallback){
        // here be tests
    });
});

在此测试中,我可以通过在 describe 函数中的任意位置添加 this.timeout(VALUE); 来更改超时。

但是,除了 timeout 值之外,还有许多其他 Mocha options 可以从命令行或位于测试文件夹 (./test/mocha.opts) 中的 mocha.opts 文件中以独占方式声明。

我想要的是在运行时更改其中一些选项(例如,reporter),而不是在命令行/mocha.opts 文件中。

根据我对可能性的研究,我发现有 an article explaining how you can use mocha programmatically,它允许在运行时更改这些选项,但您需要自己创建 Mocha 实例,而在普通测试中则没有不能直接访问 Mocha 实例。

那么,有没有办法从现有测试中获取 Mocha 实例并在测试期间的运行时更改其中一些选项,例如 reporter

我想要一个不需要以任何方式修改Mocha 源代码的选项(我想我可以篡改Mocha 实例以实现一种直接在Mocha构造函数)。

【问题讨论】:

  • 看起来您想关注那篇您自己创建 mocha 实例的文章。
  • @kevzettler 什么文章?我链接的那个?那需要您自己创建Mocha 实例和Mocha 运行器(添加测试文件等),而我想从命令行使用mocha myTest.js 并将myTest.js 挂钩到Mocha 实例并能够在运行时修改选项
  • @kevzettler 你能详细说明如何在答案中做到这一点吗?

标签: javascript automation mocha.js


【解决方案1】:

实现这一目标的最佳方法是根据您已经引用的 wiki 链接使用 Mocha,该链接以编程方式使用 Mocha。

因此,对于您关于更改 reporter 参数的询问,这里有一个简短的示例,可以满足您的要求,以便针对理论上已经存在的名为 test-file-a.js 的文件运行测试,该文件包含您的测试:

var Mocha = require('mocha'),
    mocha = new Mocha(),
    path = require('path');

mocha.addFile(path.join(__dirname, 'test-file-a.js'));

mocha
    .reporter('list')
    .run();

除了您可以使用许多其他选项之外,还有一些事件侦听器,例如 test,您可能希望在测试期间执行某些操作,例如:

mocha
    .reporter('list')
    .ui('tdd')
    .bail()
    .timeout(10000) 
    .run()      
    .on('test', function(test) {
        if (test.title === 'some title that you want here') {
            //do something
        }
    });

请注意,您可以为每个 Mocha 实例定义将再次运行测试套件的选项,但不能在测试套件的运行时,例如,如果您开始测试 @ 987654326@ 与上面的选项 reporter('list') 一样,您无法在测试运行到其他内容时更改它,例如您可以使用 timeout 选项执行 this.timeout()

因此,您必须每次都用不同的选项实例化一个新的Mocha 实例,就像上面的示例一样。

【讨论】:

    【解决方案2】:

    不,你不能。不更改代码。

    简而言之,mocha 是在您无法通过测试访问的范围内创建的。如果不详细说明,您范围中提供的对象无法更改您想要的选项。 (你不能这样做:link

    但是有一种方法可以定义您自己的reporter并为每个测试自定义输出:

    创建一个名为 MyCustomReporter.js 的文件:

    'use strict';
    
    module.exports = MyCustomReporter;
    
    function MyCustomReporter (runner) {
    
        runner.on('start', function () {
            var reporter = this.suite.suites["0"].reporter;
            process.stdout.write('\n');
    
        });
    
        runner.on('pending', function () {
                process.stdout.write('\n  ');
        });
    
        runner.on('pass', function (test) {
            var reporter = this.suite.useReporter;
            if(reporter == 'do this') {
            }
            else if(reporter == 'do that'){
            }
            process.stdout.write('\n  ');
            process.stdout.write('passed');
        });
    
        runner.on('fail', function () {
            var reporter = this.suite.useReporter;
            process.stdout.write('\n  ');
            process.stdout.write('failed ');
        });
    
        runner.on('end', function () {
            console.log();
        });
    }
    

    运行mocha时,将MyCustomReporter.js的路径作为reporter参数传递(不带.js),例如:

    mocha --reporter "/home/user/path/to/MyCustomReporter"
    

    默认的 mocha 脚本实际上会尝试在默认文件(在 lib/reporters 下)中找不到报告文件,github link

    最后,在您的测试中,您可以传递一些参数来自定义报告器的输出:

    var assert = require('assert');
    describe('Array', function() {
      describe('#indexOf()', function() {
          this.parent.reporter = 'do this';
        it('should return -1 when the value is not present', function() {
            this.runnable().parent.useReporter = 'do this';
            assert.equal([1,2,3].indexOf(4), -1);
        });
      });
    });
    

    【讨论】:

    • 我接受了这样一个事实的答案,即您确实无法从另一个上下文中获取实例,并且the link you referenced 是我更好地理解这一点的好资源。但是,对我来说,您解释如何创建自定义报告器的部分是漫无目的的。我已经有了一个,我想在运行时从默认报告器 (list) 切换到自定义报告器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多