Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your asynchronous test generates an exception, on failed <code>expect()</code>ations, that cannot be captured by <code>it()</code> because the exception is thrown outside of <code>it()</code>'s scope.</p> <p>The captured exception that you see displayed is captured using <code>process.on('uncaughtException')</code> under node or using <code>window.onerror()</code> in the browser. </p> <p>To fix this issue, you need to capture the exception within the asynchronous function called by <code>setTimeout()</code> in order to call <code>done()</code> with the exception as the first parameter. You also need to call <code>done()</code> with no parameter to indicate success, otherwise mocha would report a timeout error because your test function would never have signaled that it was done:</p> <pre><code>window.expect = chai.expect; describe( 'my test', function() { it( 'should do something', function ( done ) { // done() is provided by it() to indicate asynchronous completion // call done() with no parameter to indicate that it() is done() and successful // or with an error to indicate that it() failed setTimeout( function () { // Called from the event loop, not it() // So only the event loop could capture uncaught exceptions from here try { expect( true ).to.equal( false ); done(); // success: call done with no parameter to indicate that it() is done() } catch( e ) { done( e ); // failure: call done with an error Object to indicate that it() failed } }, 100 ); // returns immediately after setting timeout // so it() can no longer catch exception happening asynchronously } } </code></pre> <p>Doing so on all your test cases is annoying and not DRY so you might want to provide a function to do this for you. Let's call this function <code>check()</code>:</p> <pre><code>function check( done, f ) { try { f(); done(); } catch( e ) { done( e ); } } </code></pre> <p>With <code>check()</code> you can now rewrite your asynchronous tests as follows:</p> <pre><code>window.expect = chai.expect; describe( 'my test', function() { it( 'should do something', function( done ) { setTimeout( function () { check( done, function() { expect( true ).to.equal( false ); } ); }, 100 ); } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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