Here is what I have:
it('invalid use', () => {
Matcher(1).case(1, () => {});
});
I am trying to ensure that the case
method throws an exception after a delay. How can I specify this for Mocha/Chai so that the test passes only if an exception is thrown (and should fail otherwise)?
I need to emphasize that the case
method cannot be modified in any way.
To simulate the behavior for testing purposes, consider the following equivalent code:
it('setTimeout throw', _ => {
setTimeout(() => { throw new Error(); }, 1); // This line must remain unchanged
});
I attempted the following approach:
it('invalid use', done => {
Matcher(1).case(1, () => {});
// Call the done callback after 'case' may throw an error
setTimeout(() => done(), MatcherConfig.execCheckTimeout + 10);
});
However, this solution is not helping me as it produces opposite results - passing when no exception is thrown from case
(setTimeout
) and failing when an exception is thrown.
I remember reading about utilizing a global error handler, but I prefer to find a clean solution using Mocha and/or Chai, if possible (I assume Mocha already incorporates something similar behind the scenes).