title: Async await Node.js Unit Testing code using Chai and mocha
body: <p>I'm quite new to node and express. And have been trying to write test code using mocha, chai. Here's the part of the source code.</p>
  
  <p><strong>googleService.js:</strong></p>
  
  <pre><code>const axios = require('./axios');
  
  exports.hitGoogle = async (requestUrl, payload) =&gt; {
    let response = {};
    await axios.post(`${requestUrl}`, payload)
      .then((resp) =&gt; {
        response = resp;
        console.log(response);
      })
      .catch((err) =&gt; {
        console.error(err);
      });
    return response;
  };
  </code></pre>
  
  <p><strong>readGoogle.js</strong></p>
  
  <pre><code>const googleService = require('./googleService');
  
  exports.execute = async (data) =&gt; {
    console.log(`data ${JSON.stringify(data)}`);
    console.log(`Name ${data.name}`);
    console.log(`Salary ${data.salary}`);
    console.log(`Age ${data.age}`);
    const payload = { name: data.name, salary: data.salary, age: data.age };
  
    await googleService.hitGoogle('http://dummy.restapiexample.com/api/v1/create', payload)
      .then((response) =&gt; {
        if (response.status === 200 || response.status === 201) {
          console.log(response.status);
          return true;
        }
        return false;
      })
      .catch((err) =&gt; {
        console.log(err);
      });
    return true;
  };
  </code></pre>
  
  <p><strong>And here is my unit test file:</strong></p>
  
  <pre><code>const readGoogle = require('./readGoogle');
  
  const jmsPayload = { age: '23', name: 'test', salary: '123' };
  
  describe('Google ', () =&gt; {
    it('POST: Google.', async () =&gt; {
       readGoogle.execute(jmsPayload).then((result) =&gt; {
       results = result;
      console.log(`Final Result : ${results.toString()}`);
    });
  });
  </code></pre>
  
  <p>When I execute this test file, post is successful ,</p>
  
  <pre><code>hitGoogle url=http://dummy.restapiexample.com/api/v1/create
  hitGoogle payload=[object Object]
  { status: 200,
    statusText: 'OK',
  </code></pre>
  
  <p>Input values passed from test class to readGoogle.js is also read inside that,</p>
  
  <pre><code>data {"age":"23","name":"test","salary":"123"}
  Name test
  Salary 123
  Age 23
  
  **But I am getting Promise as output.** 
  
  **SO I have rendered the promised value to a normal string and getting 'true' as final output.**
  </code></pre>
  
  <p>Also,</p>
  
  <ul>
  <li><p>1)Is this is the right way of testing the async-await module?</p>
  
  <p>2) Do we need to add anything else in this test code like Settimeout
  or beforeEach or afterEach blocks?</p>
  
  <p>3) Do we need to use Sinon for spying or adding a stub to 'payload'
  in googleService.js?</p></li>
  </ul>
  
  <p>Can anyone help or suggest me the right path?</p>
  
  <p>Tried async way for executing test cases one by one in order as below,</p>
  
  <pre><code>const readGoogle = require('./readGoogle');
  
  const jmsPayload = { age: '23', name: 'test', salary: '123' };
  
  const common = require('./common');
  const chaiHttp = common.chaiHttp;
  common.chai.use(chaiHttp);
  
  describe('Google ', () =&gt; {
    common.mocha.before((done) =&gt; {
      // Wait for the server to start up
      setTimeout(() =&gt; {
        done();
      }, 1000);
    });
  
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
    });
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
      setTimeout(() =&gt; {
      }, 1000);
    });
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
    });
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
    });
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
    });
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
    });
    it('POST: Google.', async () =&gt; {
      const result = readGoogle.execute(jmsPayload);
      console.log(`Final Result : ${result.toString()}`);
    });
  });
  </code></pre>
  
  <p>But then I am getting,</p>
  
  <p><strong>Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves</strong></p>
  
  <p>Thanks in advance</p>