title: Testing asynchronously returned data
body: <p>I have the following example:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;Mocha&lt;/title&gt;
      &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;mocha.css&quot; /&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id=&quot;mocha&quot;&gt;&lt;/div&gt;
      &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/mocha/3.1.2/mocha.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js&quot;&gt;&lt;/script&gt;
  
      &lt;script src=&quot;https://unpkg.com/vue/dist/vue.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/vue-resource/1.0.3/vue-resource.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/sinon.js/1.15.4/sinon.js&quot;&gt;&lt;/script&gt;
  
      &lt;script&gt;mocha.setup('bdd');&lt;/script&gt;
      &lt;script&gt;
       &quot;use strict&quot;;
       var assert = chai.assert;
       var should = chai.should();
  
       var vm = new Vue({
       data: {
           message: &quot;Hello&quot;
       },
  
       methods: {
           loadMessage: function() {
           this.$http.get(&quot;/get&quot;).then(
               function(value) {
               this.message = value.body.message;
               });
           },
       }
       });
       describe('getMessage', function() {
       let server;
       beforeEach(function () {
           server = sinon.fakeServer.create();
       });
  
       it(&quot;should get the message&quot;, function(done) {
           server.respondWith([200, { 'Content-Type': 'application/json' },
                   JSON.stringify({message: &quot;Test&quot;})]);
  
           vm.message.should.equal(&quot;Hello&quot;);
           vm.loadMessage();
           server.respond();
           setTimeout(function() {
           // This one works, but it's quirky and a possible error is not well represented in the HTML output.
           vm.message.should.equal(&quot;Test&quot;);
           done();
           }, 100);
  
           // This one doesn't work
           //vm.message.should.equal(&quot;Test&quot;);
       });
       });
      &lt;/script&gt;
      &lt;script&gt;
        mocha.run();
      &lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>I want to test that Vue asynchronously gets data from the server. Though, I mock out the actual HTTP request with Sinon FakeServer.</p>
  <p>Naturally, directly after the call to <code>loadMessage</code>, the message is not yet set. I could use a timeout function for the test, but I believe there should be a better method. I've looked into <code>respondImmediately</code>, but it did not change. Also, there is the possibility to call a done() function. However, as I understand this, this done would need to be called within the <code>loadMessage</code> function, hence modifying the code under test.</p>
  <p>What is the correct approach to handle this problem?</p>
  <p>Edit: I have found at least a partial solution, but it seems to be still messy: call the done() function in the mocha unit test. When the assertion fails, it is at least shown in the HTML output. However, the assertion message is not as clear as in a normal test. Also, the technique still seems messy to me.</p>