Vad*_*der 9 javascript testing mocha.js node.js
有没有办法在mochajs中列出测试运行器收集的所有测试而不执行它们?
例如,如果有规格看起来像:
describe('First', function() {
it('should test something', function() {
...
})
});
describe('Second', function() {
it('should test something else', function() {
...
})
});
Run Code Online (Sandbox Code Playgroud)
那么我想获得类似于测试记者生成的输出的控制台输出,但是没有执行实际测试,如下所示:
First
should test something
Second
should test something else
Run Code Online (Sandbox Code Playgroud)
UPD:
目前我用正则表达式提取所有describes和its,但寻找更清洁的解决方案.
mocha-list-tests 包很有用,但仅适用于 BDD 样式describe()和it(),如果您进行.skip()任何测试,它会中断,因为它模拟it().
如果您需要克服这些问题中的任何一个或获取有关测试的其他信息,那么自己执行此操作的一种方法是利用 Mocha 的根before()挂钩。这将在 Mocha 加载所有文件之后但在执行任何测试之前执行,因此您需要的所有信息都在此时存在。
这样,很容易在--list-only命令行选项中打补丁以切换测试运行的行为,而无需添加或更改任何其他内容。
关键是this在before()钩子里是摩卡的Context,而.test那个是指钩子本身。所以this.test.parent是指根套件。从那里,您可以沿着.suites数组树和.tests每个套件的数组向下走。
收集了你想要的任何东西后,你必须输出它并退出进程以阻止 Mocha 继续。
鉴于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;
}
Run Code Online (Sandbox Code Playgroud)
和test.js:
#!/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() {
// ...
});
});
});
Run Code Online (Sandbox Code Playgroud)
然后mocha正常运行会给你你期望的测试结果:
foo
bar
? should do something
baz
- should do something else
? should do another thing
2 passing (8ms)
1 pending
Run Code Online (Sandbox Code Playgroud)
但是运行mocha --list-only会给你(不运行任何测试):
Suite (root)
Suite foo
Suite bar
Test should do something
Suite baz
Test should do something else
Test should do another thing
Run Code Online (Sandbox Code Playgroud)
根.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
};
}
});
Run Code Online (Sandbox Code Playgroud)
使用与以前相同的测试脚本会给你:
{
"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": []
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想列出(并以编程方式使用)所有测试,您需要启用dry-run报告json器+输出文件路径,如下所示:
{
"dry-run": true,
"reporter": "json",
"reporterOptions": [
"output": "./test-report.json"
]
}
Run Code Online (Sandbox Code Playgroud)
如果您想以编程方式使用测试输出:
自此提交(2021 年 8 月)起,您只需提供output文件配置选项,然后从测试处理程序中读取 json 文件。
倒霉。事实证明,JSON 报告器与它的名字相反,它不会生成纯 JSON(正如这个突出的 Mocha 问题中所讨论的)。作为解决方法,我编写了一个小脚本(此处要点),其工作原理如下:
npm run test > tests-raw.json
yarn test > tests-raw.jsonmocha --dry-run --reporter=json ... > tests-raw.json./mocha-list-tests.js tests-raw.json tests.jsontests.json现在包含所有测试结果对象的数组。mocha记录的每次调用都应该产生一个这样的对象。我找不到太多关于确切输出格式的文档,但主要只有两件事:
stats- 基本总体统计数据tests- 所有找到的测试(参见Test#serialize 方法)failures, pending,passes| 归档时间: |
|
| 查看次数: |
1405 次 |
| 最近记录: |