【发布时间】:2019-08-27 00:39:12
【问题描述】:
如果条件是异步函数调用,我如何有条件地执行 mocha 测试?
我尝试基于synchronous example 进行异步实现。在下面的两个 sn-ps 中,我预计 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() 行更改为执行同步函数调用,则代码有效。
【问题讨论】:
标签: javascript mocha.js