如果条件是异步函数调用,我如何有条件地执行mocha测试?
我尝试过基于同步示例来实现异步实现。在下面的两个片段中,我预期some test会被执行,因为asyncCondition()返回的承诺被解析为true。
首先,我尝试await条件:
const assert = require('assert');
const asyncCondition = async () => Promise.resolve(true);
describe('conditional async test', async () => {
const condition = await asyncCondition();
(condition ? it : it.skip)('some test', () => {
assert.ok(true);
});
});结果:No tests were found。
接下来,我尝试了一个异步before钩子:
const assert = require('assert');
describe('conditional async test', async () => {
let condition;
before(async () => {
condition = await asyncCondition();
});
(condition ? it : it.skip)('some test', () => {
assert.ok(true);
});
});结果:Pending test 'some test'。
如果更改行const condition = await asyncCondition()以执行同步函数调用,则代码可以工作。
发布于 2019-04-06 04:21:40
摩卡运行周期运行所有describe回调并同步收集测试,因此只有在执行describe回调时,才能使用同步可用的条件在it和it.skip之间切换。
如果条件是异步函数调用,我如何有条件地执行mocha测试?
.skip()到..。
...tell Mocha简单地忽略了这些套件和测试用例。
.skip()可以在before中用于跳过测试套件中的所有测试:
const assert = require('assert');
const asyncCondition = async () => Promise.resolve(false);
describe('conditional async test', function () {
before(async function () {
const condition = await asyncCondition();
if (!condition) {
this.skip(); // <= skips entire describe
}
});
it('some test', function () {
assert.ok(true);
});
});...or可以在单个测试中使用,只跳过该测试:
const assert = require('assert');
const asyncCondition = async () => Promise.resolve(false);
describe('conditional async test', function () {
let condition;
before(async function () {
condition = await asyncCondition();
});
it('some test', function () {
if (!condition) this.skip(); // <= skips just this test
assert.ok(true);
});
});https://stackoverflow.com/questions/55529793
复制相似问题