title: How to ensure Database Query finishes before unit tests executed in QUnit
body: <p>I am trying to run unit tests on my web app using QUnit and I need a way to setup the databases test data before the unit tests are run.  Manually entering and erasing test data from the Database for each test is a painfully slow and repetitive. </p>
  
  <p>I coded some temporary request handlers on the server-side which handle the 'BEFORE' requests shown in the code below.</p>
  
  <p>The problem is that modifying the database takes to long, so the QUnit tests start running before the database test data has had a chance to be updated.</p>
  
  <p>The best reference I could find is <a href="https://api.qunitjs.com/QUnit.module/" rel="nofollow noreferrer">https://api.qunitjs.com/QUnit.module/</a> (at the bottom of the page).  It shows that a database connection can be opened and closed in the 'before' and 'after' functions using promises.  However, the example is too brief and doesn't provide enough information to do what I am trying to do.</p>
  
  <p>I have thought of nesting my JQuery requests, so that the test code is only executed after receiving the servers response (after updating the database).  However, this makes the test code hard to read, repetitive and it ignores the use of the 'before' and 'after' hooks provided by QUnit.</p>
  
  <p>Any suggestions?   </p>
  
  <p>Below is the test code I currently have for setting up the database for the unit tests.</p>
  
  <pre><code>QUnit.module("EMD_4: updateItemQtyRequest",{
  
  before: function(){
  
      // Rule 2 setup
      $.get( "http://localhost:8080/foodmap/EMD_4_BEFORE.do",
              {cmd:"insert into wants (email,id,qty) values ('only_wants',220,1)"}
            ); 
  
      // Rule 3 setup
      $.get( "http://localhost:8080/foodmap/EMD_4_BEFORE.do",
              {cmd:"insert into wants (email,id,qty) values ('wants_and_hasWanted',219,1)"}
            );
      $.get( "http://localhost:8080/foodmap/EMD_4_BEFORE.do",
              {cmd:"insert into has_wanted (email,id) values ('wants_and_hasWanted',220)"}
            );  
  },
  after: function(){
     // cleanup database
  }
  ...Unit Tests ..
  </code></pre>