title: Mocha testing stubbed ajax call with a .then
body: <p>I've got a jquery ajax call that looks something like this:</p>
  
  <pre><code>$.ajax({
    type: "GET",
    dataType: 'json',
    data: data,
    url: url
  }).then((data, successFlag, xhr) =&gt; {
    this.props.someFunc(data)
  })
  </code></pre>
  
  <p>In my test file, I have the jquery ajax call stubbed out with sinon and it returns a resolved promise with data:</p>
  
  <pre><code>sinon.stub($, 'ajax')
    .returns(Promise.resolve({ data: 'test data' }))
  </code></pre>
  
  <p>And I'm also spying on my someFunc(data) function. In my test, I'm calling a function that makes the ajax call, and then expecting my someFunc(data) to be called. However, the expectation fails, but when I put a console log in my someFunc(data) function, I can see that it is clearly being called:</p>
  
  <pre><code>component.instance().makeAjaxCall()
  expect($.ajax.calledOnce).to.be.true // passes
  expect(someFuncSpy.calledOnce).to.be.true // fails
  </code></pre>
  
  <p>Now I assume that it's failing because it's checking the expectation before then .then executes and I tried looking up some solutions dealing with testing with promises but nothing I've tried works so far (or I'm implementing it wrong). How can make sure that the .then finishes executing before I check the expectation?</p>