How can I conditionally execute mocha tests based if the condition is an asynchronous function call?
I have tried to make an asynchronous implementation based on a synchronous example. In both snippets below I expected some test
to be executed since the promise returned by asyncCondition()
is resolved to true
.
First, I tried to await
the condition:
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);
});
});
Result: No tests were found
.
Next, I tried an asynchronous before
hook:
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);
});
});
Result: Pending test 'some test'
.
The code works if if the line const condition = await asyncCondition()
is changed to execute a synchronous function call.