title: Asserting a callback was called with Jest (without React)?
body: <p>I have a suite of tests but something is just not clicking regarding callback assertions. My feeling is that the done() parameter needs to be woven in, but I'm doing it wrong. </p>
  
  <p>I basically have two function structures, where the callback is either nested inside of a single then statements, or a then inside of another then:</p>
  
  <pre><code>function foo(cb) {
     return fetch('foo.com')
            .then(
                (res)=&gt;{
                 res
                 .json()
                 .then(
                   (data) =&gt;cb(data)
                })
            .catch(err =&gt; console.error(err))
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>function foo(cb) {
       return fetch('foo.com')
              .then(()=&gt;cb())
              .catch(err =&gt; console.error(err))
  }
  </code></pre>
  
  <p>I'm looking to assert that the callback was called in both cases. </p>
  
  <p>I have tried</p>
  
  <pre><code>describe('ugh why can't I figure this out'?, () =&gt; {
  it('is confusing', (done) =&gt; {
  
  const testCb = jest.fn()
  
  foo(testCb);
  
  expect(testCb).toHaveBeenCalled()
  
  done();
  
  //failed: expected testCb to be called, but it was not called
  }
  })
  </code></pre>
  
  <p>I'm not sure how to move forward- I'm not a fan of the spaghetti on the wall approach, so I'd rather understand how to implement jest for testing async code before I just start switching in different syntax. The above code seems like it should work because the callback is part of the function execution, so if the higher order function executes, the callback would necessarily be executed, and should have been "called". Obviously this reasoning is wrong since the test isn't passing, but I'm not quite seeing the way around this. </p>
  
  <p>Thanks very much for any direction/insight :). </p>
  
  <p>This jest example seems to match mine- what am I missing? </p>
  
  <pre><code>describe('drinkAll', () =&gt; {
    test('drinks something lemon-flavored', () =&gt; {
      let drink = jest.fn();
      drinkAll(drink, 'lemon');
      expect(drink).toHaveBeenCalled();
    });
  
    test('does not drink something octopus-flavored', () =&gt; {
      let drink = jest.fn();
      drinkAll(drink, 'octopus');
      expect(drink).not.toHaveBeenCalled();
    });
  });
  </code></pre>