title: Is it possible to run mocha test suite from node endpoint?
body: <p>Our team has built an API for providing internal web services to our organization.  I have built a mocha test suite for testing the code base, and am currently running it via the CLI.  </p>
  
  <p>We have a corporate monitoring service that regularly tests a '/webcheck' endpoint for each server, and notifies us if any of these servers are down.</p>
  
  <p>Currently that endpoint simply responds with a 'overall_status_code: ok' string, which tells us that the server is running, and nothing more.  The other endpoints could be messed up, and we wouldn't know it.  </p>
  
  <p>I'd like to tie this endpoint into my mocha test suite such that it only returns the 'overall_status_code: ok' if all of the tests pass.  </p>
  
  <p>Anyone have hints how I could do this?  Basically, how to invoke my tests from within node?</p>
  
  <p>Here's my test code in case you're interested: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const expect = require('chai').expect;
  const app = require('../server/app.js');
  const supertest = require('supertest');
  
  const request = supertest.agent(app);
  
  describe('Active Directory Module', () =&gt; {
      it('/ad/members endpoint should return: \n          -&gt; an array of members for a given group \n          -&gt; each element returned should be an object \n          -&gt; John Doe should be the first user in the array', (done) =&gt; {
          request.get('/ad/members?group=%27CHQ%20Service%20Management%27')
          .expect(200)
          .end((err, res) =&gt; {
              let result = JSON.parse(res.text);
              expect(Array.isArray(result)).to.be.true;
              expect(typeof(result[0]) === 'object').to.be.true;
              expect(result[0].name === 'John Doe').to.be.true;
              done();
          })
      })
      it('/ad/membership endpoint should return: \n          -&gt; an array of groups for a given user \n          -&gt; each element returned should be an object \n          -&gt; \'Domain Users\' should be the first element in the array', (done) =&gt; {
          request.get('/ad/membership?user=%27chq-mikeru%27')
          .expect(200)
          .end((err, res) =&gt; {
              let result = JSON.parse(res.text);
              // let result = res;
              expect(Array.isArray(result)).to.be.true;
              expect(typeof(result[0]) === 'object').to.be.true;
              expect(result[0].name === 'Domain Users').to.be.true;
              done();
          })
      })
      it('/ad/userinfo endpoint should return: \n          -&gt; an object\n          -&gt; Should include property called \'Given Name\'\n          -&gt; given \'chq-stephenro\' the returned object should contain a property \'UserPrincipleName\' with value \'chq-stephenro@corp.company.com\'', (done) =&gt;{
          request.get('/ad/userinfo?user=%27chq-stephenho%27')
          .expect(200)
          .end((err, res) =&gt; {
              let result = JSON.parse(res.text);
              expect(typeof(result) === 'object').to.be.true;
              expect(result['GivenName']).to.not.be.undefined;
              expect(result['UserPrincipalName'] === 'chq-stephenro@corp.company.com').to.be.true;
              done();
          })
      })
  })
  
  describe('Database Querying Module', () =&gt; {
      it('/db/ci-co-conflict-checker should return \n          --&gt; an array\n          --&gt; of arrays\n          --&gt; For each element:\n                    * the first, third, and fourth elements should be numbers\n                    * the second element should be a string\n                    * the fifth element should be a string', (done) =&gt; {
          request.get('/db/ci-co-conflict-checker/?cis=%27CA%20Service%20Catalog%27,%27CA%20Service%20Desk%20Manager%27&amp;start=1533106860&amp;end=1535699040')
          .expect(200)
          .end((err, res) =&gt; {
              let result = res.body;
              expect(Array.isArray(result)).to.be.true;
              expect(Array.isArray(result[0])).to.be.true;
              for ( var i = 0; i &lt; result.length; i++ ) {
                  for (var j = 0; j &lt; result[i].length; j++ ) {
                      if ( j === 0 || j === 2 || j === 3) {
                          expect(typeof(result[i][j]) === 'number').to.be.true;
                      }
                      if ( j === 1 || j === 4 ) {
                          expect (typeof(result[i][j]) === 'string').to.be.true;
                      }
                  }
              }
              done();
          })
      })
  })</code></pre>
  </div>
  </div>
  </p>