title: How do I write Mocha tests dependent on other Mocha tests?
body: <p>I have a series of tests I'm trying to run on an Express REST API, emphasis on the word <em>series</em>, because they need to be sequential to some degree. Let me explain:</p>
  
  <p>Tests, in order:</p>
  
  <ol>
  <li>API is accessible?</li>
  <li>API connects and authenticates properly?</li>
  <li>Auth tokens can then be used to write to the data store?</li>
  <li>Auth tokens can then be used to retrieve data just written to data store?</li>
  <li>Auth tokens can then be used to change data written (test if it worked with another retrieve)?</li>
  </ol>
  
  <p>Note that these steps really only make any sense if they happen in order, and yet Mocha doesn't seem to run tests in order. I could make one test that runs Step 1, then another that runs Steps 1-2, then another that runs Steps 1-3, and so on, but that's definitely not DRY. I could also try to set up a chain of <code>before</code> and <code>after</code>s, but that doesn't seem up to conventions.</p>
  
  <p><strong>Is there a proper way of running Mocha tests checking sequential steps in an process?</strong></p>
  
  <p>I'm trying to <strong>get rid of this pattern</strong> of just adding additional steps to repeated tests...</p>
  
  <pre><code>describe('Api', () =&gt; {
      it('should be accessible', (done) =&gt; {
          // Try to connect:
          Api.connect((error, conn) =&gt; {
              done(error);
          });
      });
  
      it('should connect and authenticate properly', (done) =&gt; {
          // Try to connect:
          Api.connect((error, api) =&gt; {
              if (error) done(error);
              // Then try to authenticate:
              api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) =&gt; {
                  done(error);
              });
          });
      });
  
      it('should allow for data to be written to the data store', (done) =&gt; {
          // Try to connect:
          Api.connect((error, api) =&gt; {
              if (error) done(error);
              // Then try to authenticate:
              api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) =&gt; {
                  if (error) done(error);
                  // Then try to write data:
                  conn.write(generateTestData(), (error, res) =&gt; {
                      done(err);
                  });
              });
          });
      });
  
      it('should allow for written data to be read from the data store', (done) =&gt; {
          // Try to connect:
          Api.connect((error, api) =&gt; {
              if (error) done(error);
              // Then try to authenticate:
              api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) =&gt; {
                  if (error) done(error);
                  let testData = generateTestData();
                  // Then try to write data:
                  conn.write(testData, (error, res) =&gt; {
                      if (error) done(error);
                      // Then try to read data:
                      conn.readLast((error, res) =&gt; {
                          if (error) done(error);
                          assert.equal(testData, res);
                      });
                  });
              });
          });
      });
  });
  </code></pre>