title: Node.js - Mock result of a promise
body: <p>I want to mock the result of a function within a node module so that i can run assertions.
  Considering the following node module:</p>
  
  <pre><code>const doPostRequest = require('./doPostRequest.js').doPostRequest;
  const normalizeSucessResult = require('./normalizer.js').normalizeSucessResult;
  const normalizeErrorResult = require('./normalizer.js').normalizeErrorResult;
  
  exports.doPost = (params, postData) =&gt; {
    return doPostRequest(params, postData).then((res) =&gt; {
      const normalizedSuccessResult = normalizeSucessResult(res);
      return normalizedSuccessResult;
    }).catch((err) =&gt; {
      const normalizedErrorResult = normalizeErrorResult(err);
      return normalizedErrorResult;
    })
  }
  </code></pre>
  
  <p>The function <code>doPostRequest</code> returns a promise. How can i fake the return value of this promise so that i can assert if <code>normalizeSucessResult</code> has been called?
  So for i have tried:</p>
  
  <pre><code>const normalizeSucessResult = require('./normalizer.js');
  const doPostRequest = require('./doPostRequests.js');
  const doPost = require('./doPost.js');
  
  it('runs a happy flow scenario', async () =&gt; {
    let normalizeSucessResultStub = sinon.stub(normalizeSucessResult, 'normalizeSucessResult');
    let postData = { body: 'Lorum ipsum' };
    let params = { host: 'someUrl', port: 433, method: 'POST', path: '/' };
  
    sinon.stub(doPostRequest, 'doPostRequest').resolves("some response data"); //Fake response from doPostRequest
  
    return doPost.doPost(params, postData).then((res) =&gt; { //res should be equal to some response data
      expect(normalizeSucessResultStub).to.have.been.calledOnce;
      expect(normalizeSucessResultStub).to.have.been.with("some response data");
    });
  });
  </code></pre>
  
  <p>The <code>doPostRequest</code> module looks like this:</p>
  
  <pre><code>const https = require('https')
   module.exports.doPostRequest = function (params, postData) {
    return new Promise((resolve, reject) =&gt; {
      const req = https.request(params, (res) =&gt; {
        let body = []
        res.on('data', (chunk) =&gt; {
          body.push(chunk)
        })
        res.on('end', () =&gt; {
          try {
            body = JSON.parse(Buffer.concat(body).toString())
          } catch (e) {
            reject(e)
          }
          resolve(body)
        })
      })
      req.on('error', (err) =&gt; {
        reject(err)
      })
      if (postData) {
        req.write(JSON.stringify(postData))
      }
      req.end()
    })
  }
  </code></pre>