有没有办法获取传递给describe的消息数组?

我想根据下面的testList调用中作为消息传递的值动态创建describe数组。

示例测试



<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.min.css" rel="stylesheet"/>
<div id="mocha"></div>
<script>
    mocha.setup('bdd');
</script>
<script>
    mocha.checkLeaks();
    //mocha.globals(['jQuery']);
</script>
<script>
    var expect = chai.expect;
    var testList = ['methodTrue', 'methodFalse', 'methodIdentity'];
    var testObject = {
        methodTrue: function () {return true;},
        methodFalse: function () {return false;},
        methodIdentity: function (n) {return n;}
    }
    describe('testObject', function () {
        describe('methodTrue', function () {
            it('should be a method', function () {
                expect(testObject.methodTrue).to.be.a('function');
            });
        });
        describe('methodFalse', function () {
            it('should be a method', function () {
                expect(testObject.methodFalse).to.be.a('function');
            });
        });
        describe('methodIdentity', function () {
            it('should be a method', function () {
                expect(testObject.methodIdentity).to.be.a('function');
            });
        });
        it('should have a method for every test', function () {
            Object.keys(testObject).forEach(function (v, i) {
                expect(testList.indexOf(v), 'no test for ' + v).to.be.above(-1);
            });
        });
    });
    mocha.run();
</script>

最佳答案

您可能会看一下Mocha的来源,并弄清楚如何进行测试。但是,这是一种不依赖于内部知识且不会因内部结构发生变化而中断的方法。该策略是用您自己的函数替换describe,该函数记录传递给它的内容,以便您以后使用。我在命令行上使用过Mocha,但是在要在Node中运行的套件中与在浏览器中运行的套件之间没有区别。

var blocks = [];

function wrapperDescribe() {
    // It is generally unsafe to just leak `arguments` objects. So we
    // slice it to make a copy before pushing it into `blocks`.
    var args = Array.prototype.slice.call(arguments);
    blocks.push(args);
    describe.apply(this, args);
}

(function () {
    // We do not do this at the top level because it would modify a
    // global used by all Mocha files. Whether or not you do want this
    // depends on the needs to you project.
    var describe = wrapperDescribe;

    function fn () {}

    describe("one", function () {
        it("test 1", fn);
        it("test 2", fn);
    });

    describe("two", function () {
        it("test 1", fn);
        it("test 2", fn);
    });
}());

console.log(blocks);


输出:

$ ./node_modules/.bin/mocha
[ [ 'one', [Function] ], [ 'two', [Function] ] ]


  one
    ✓ test 1
    ✓ test 2

  two
    ✓ test 1
    ✓ test 2


  4 passing (6ms)


在运行测试之前输出数组,这对于Mocha是正常的。 (Mocha首先读取所有测试,执行所有describe回调,然后运行测试。)

为了使其仅在describe块的子集上起作用,您不能覆盖describe,而是根据需要直接调用wrapperDescribe

关于javascript - 获取给定测试的mocha`describe`调用列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37495065/

10-11 18:50