sinon spy is not called in a stub with async function

Api.get is async function and it returns a promise, so to emulate async call in test you need to call resolves function not returns:

Causes the stub to return a Promise which resolves to the provided value. When constructing the Promise, sinon uses the Promise.resolve method. You are responsible for providing a polyfill in environments which do not provide Promise.

sinon
  .stub(Api, 'get')
  .resolves('ok');

your console.log(data) always happens because your Promise does resolve, it just does so after the test has finished, which is why the assertion fails.

By wrapping it in a setTimeout you create another event in the loop, which allows your Promise to resolve before the test finishes, meaning that your assertion will now pass.

This is a fairly common problem when unit testing asynchronous code. Often resolved in wrapping the assertions in setImmediate and calling done from the callback of setImmediate.

https://stackoverflow.com/a/43855794/6024903