Mocha Dynamic Test Generation In Before Block Not Getting Executed
As suggested in this post , I tried the steps to create dynamic tests , but I see the actual test(test.getMochaTest()in my below implementation) not getting executed. What's that I
Solution 1:
It doesn't look like you're actually invoking the test.
Calling test.getMochaTest()
only returns the async test function in a Promise, it doesn't execute it. So your catch
block is catching errors while obtaining the function, not while executing it.
Breaking it out across multiple lines will hopefully make things clearer.
Here's what your code sample does. Notice it never executes the returned test function:
it(test.name, async () => {
const testFn = await test.getMochaTest().catch(() =>
console.error(`error while ***obtaining*** test: \t${test.name}`));
// oops - testFn never gets called!
});
And here's a corrected version where the test actually gets called:
it(test.name, async () => {
const testFn = await test.getMochaTest().catch(() =>
console.error(`error while ***obtaining*** test: \t${test.name}`));
const outcome = await testFn().catch(() =>
console.error(`error while ***executing*** test: \t${test.name}`));
});
Note: I wrote it that way with await
and catch()
to better match the format of your code sample. However, it's worth pointing out that it mixes async
/await
and Promise
syntax. More idiomatic would be to catch errors with a try/catch
block when using async
/await
.
Post a Comment for "Mocha Dynamic Test Generation In Before Block Not Getting Executed"