gpt4 book ai didi

javascript - 列出所有 mocha 测试而不执行它们

转载 作者:数据小太阳 更新时间:2023-10-29 04:50:31 27 4
gpt4 key购买 nike

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

更新:

目前我正在使用正则表达式提取所有 describeit,但正在寻找更简洁的解决方案。

最佳答案

mocha-list-tests 包很有用,但只适用于 BDD 风格的 describe()it(),如果你 它就会中断。跳过 () 任何测试,因为它模拟了 it()

如果您需要克服其中任何一个问题,或者获取有关测试的其他信息,您可以自行执行此操作的一种方法是利用 Mocha 的根 before() Hook 。这将在 Mocha 加载所有文件之后但在执行任何测试之前执行,因此您需要的所有信息都在此时存在。

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

关键是before() hook中的this是Mocha的Context,而.test其中指的是钩子(Hook)本身。所以 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;
}

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() {
// ...
});
});
});

然后正常运行 mocha 会给你预期的测试结果:

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


2 passing (8ms)
1 pending

但是运行 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

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": []
}
}

关于javascript - 列出所有 mocha 测试而不执行它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com