Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting for an Asynchronous throw in Mocha
    primarykey
    data
    text
    <p>I have a block of code that tries to reconnect to Redis if a connection breaks. If it can not re-establish connection, it throws an error. I am trying to test the block of code that throws the error, but I am unable to write a successful test using mocha and chai.</p> <p>My test looks like this:</p> <pre><code> it('throws an error when a connection can\'t be established', function (done) { var c = redisClient.newClient(); c.end(); sinon.stub(redisClient, 'newClient', function () { return { connected: false }; }); redisClient.resetConnection(c, 2, 100, function (err) { done(); }); process.on('uncaughtException', function (err) { err.message.should.equal('Redis: unable to re-establish connection'); done(); }); }); </code></pre> <p>I've tried using the assert().throws but that fails before the asynchronous throw occurs. A try/catch block also fails for the same reason. My guess is that mocha captures the exception and re-throws it because the uncaughtException block does get the error, but not before mocha has failed the test. Any suggestions?</p> <p>Edit:</p> <p>I had tried wrapping the call in a function:</p> <pre><code>var a = function() {redisClient.resetConnection(c, 2, 100, function () { done('Should not reach here'); }); }; expect(a).to.throw(/unable to re-establish connect/); </code></pre> <p>and I get the following:</p> <pre><code>✖ 1 of 5 tests failed: 1) RedisClient .resetConnection emits an error when a connection can't be established: expected [Function] to throw an error </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload