Javascript 列出所有mocha测试,但不执行它们

Javascript 列出所有mocha测试,但不执行它们,javascript,node.js,testing,mocha.js,Javascript,Node.js,Testing,Mocha.js,mochajs中是否有一种方法可以列出测试运行程序收集的所有测试,而不执行它们 例如,如果有如下规格: describe('First', function() { it('should test something', function() { ... }) }); describe('Second', function() { it('should test something else', function() { ...

mochajs中是否有一种方法可以列出测试运行程序收集的所有测试,而不执行它们

例如,如果有如下规格:

describe('First', function() {
    it('should test something', function() {
        ...
    })
});

describe('Second', function() {
    it('should test something else', function() {
        ...
    })
});
然后,我希望获得与测试报告器生成的输出类似的控制台输出,但不执行实际测试,如下所示:

First
    should test something
Second
    should test something else
UPD:


目前,我正在使用正则表达式提取所有
descripe
s和
it
s,但正在寻找更干净的解决方案。

将所有descripe块包装在descripe块中,然后跳过它

describe.skip('Outline', function() {
    describe('First', function() {
        it('should test something', function() {
            ...
        })
    });

    describe('Second', function() {
        it('should test something else', function() {
            ...
        })
    });
});

摩卡列表测试
套餐

我不骗你,这有一个单独的包:

样本输出:

{
  "suites": [
    "a1",
    "a2"
  ],
  "tests": [
    "a1.a11",
    "a1.a12",
    "a2.a21",
    "a2.a22"
  ],
  "tree": {
    "a1": {
      "a11": true,
      "a12": true
    },
    "a2": {
      "a21": true,
      "a22": true
    }
  }
}
对于此测试文件:

main.js

#!/usr/bin/env node

const assert = require('assert');
const fs = require('fs');

describe('a1', function() {
  it('a11', function() {
    assert.equal(1, 1);
    fs.writeFileSync('abc', 'def', 'utf8');
  });
  it('a12', function() {
    assert.equal(1, 2);
  });
});
describe('a2', function() {
  it('a21', function() {
    assert.equal(1, 1);
  });
  it('a22', function() {
    assert.equal(1, 2);
  });
});
文件
abc
没有创建,所以我知道没有执行测试

这可能是使用API摩卡咖啡,在:


测试mocha@6.2.2,摩卡咖啡单-tests@1.0.2,节点v10.15.1。

摩卡列表测试包很有用,但仅适用于BDD样式
descripe()
it()
,如果
,它会中断。跳过()
任何测试,因为它模拟
it()

如果您需要克服这两个问题中的任何一个,或者获取有关测试的其他信息,您自己可以使用Mocha的root
before()
hook来完成这项工作。这将在Mocha加载所有文件之后执行,但在它执行任何测试之前执行,因此此时您需要的所有信息都存在

通过这种方式,很容易在
--list only
命令行选项中进行修补,以切换测试运行的行为,而无需添加或更改任何其他内容

关键是
before()中的
this
钩子是Mocha的
上下文
,它的
.test
是指钩子本身。因此
this.test.parent
引用根套件。从那里,您可以沿着
.suites
数组树,以及每个套件的
.tests
数组

收集完你想要的东西后,你必须输出它并退出这个过程以阻止摩卡咖啡继续

纯文本示例 给定root.js

#!/bin/env mocha

before(function() {
    if(process.argv.includes('--list-only')) {
        inspectSuite(this.test.parent, 0);
        process.exit(0);
    }
    // else let Mocha carry on as normal...
});

function inspectSuite(suite, depth) {
    console.log(indent(`Suite ${suite.title || '(root)'}`, depth));

    suite.suites.forEach(suite => inspectSuite(suite, depth +1));
    suite.tests.forEach(test => inspectTest(test, depth +1));
}

function inspectTest(test, depth) {
    console.log(indent(`Test ${test.title}`, depth));
}

function indent(text, by) {
    return '    '.repeat(by) + text;
}
#!/bin/env mocha

describe('foo', function() {
    describe('bar', function() {
        it('should do something', function() {
            // ...
        });
    });

    describe('baz', function() {
        it.skip('should do something else', function() {
            // ...
        });

        it('should do another thing', function() {
            // ...
        });
    });
});
test.js

#!/bin/env mocha

before(function() {
    if(process.argv.includes('--list-only')) {
        inspectSuite(this.test.parent, 0);
        process.exit(0);
    }
    // else let Mocha carry on as normal...
});

function inspectSuite(suite, depth) {
    console.log(indent(`Suite ${suite.title || '(root)'}`, depth));

    suite.suites.forEach(suite => inspectSuite(suite, depth +1));
    suite.tests.forEach(test => inspectTest(test, depth +1));
}

function inspectTest(test, depth) {
    console.log(indent(`Test ${test.title}`, depth));
}

function indent(text, by) {
    return '    '.repeat(by) + text;
}
#!/bin/env mocha

describe('foo', function() {
    describe('bar', function() {
        it('should do something', function() {
            // ...
        });
    });

    describe('baz', function() {
        it.skip('should do something else', function() {
            // ...
        });

        it('should do another thing', function() {
            // ...
        });
    });
});
然后正常运行
mocha
,将得到您期望的测试结果:

  foo
    bar
      ✓ should do something
    baz
      - should do something else
      ✓ should do another thing


  2 passing (8ms)
  1 pending
但是运行
mocha--list only
将为您提供(无需运行任何测试):

JSON示例 root.js

#!/bin/env mocha

before(function() {
    let suites = 0;
    let tests = 0;
    let pending = 0;

    let root = mapSuite(this.test.parent);

    process.stdout.write(JSON.stringify({suites, tests, pending, root}, null, '    '));
    process.exit(0);

    function mapSuite(suite) {
        suites += +!suite.root;
        return {
            title: suite.root ? '(root)' : suite.title,
            suites: suite.suites.map(mapSuite),
            tests: suite.tests.map(mapTest)
        };
    }

    function mapTest(test) {
        ++tests;
        pending += +test.pending;
        return {
            title: test.title,
            pending: test.pending
        };
    }
});
使用与之前相同的测试脚本,您将获得:

{
    "suites": 3,
    "tests": 3,
    "pending": 1,
    "root": {
        "title": "(root)",
        "suites": [
            {
                "title": "foo",
                "suites": [
                    {
                        "title": "bar",
                        "suites": [],
                        "tests": [
                            {
                                "title": "should do something",
                                "pending": false
                            }
                        ]
                    },
                    {
                        "title": "baz",
                        "suites": [],
                        "tests": [
                            {
                                "title": "should do something else",
                                "pending": true
                            },
                            {
                                "title": "should do another thing",
                                "pending": false
                            }
                        ]
                    }
                ],
                "tests": []
            }
        ],
        "tests": []
    }
}

虽然这会打印出一个测试列表,但并不能真正解决问题。我不知道如何将此解决方案用于一个项目,其中测试存储在多个文件中,并且需要定期检索测试列表。您真正想要的是
--dry run
选项,该选项已被提出,但从未被合并。看,是的,这正是我想要的,可惜它没有被接受。至于信息,不,它没有使用API。它专门模拟
descripe()
It()
,然后
require()
s测试脚本,捕获这些脚本的用法。