title: How to chain promises with multiple expections in jest
body: <p>I am trying to find a clean way to write an integration test (unit test with jest which uses real resources) with jest using promise chaining but I can get my head around it.</p>
  <p>In addition of actual nesting the promises correctly I also don't know how to solve those two issues:</p>
  <ul>
  <li>How to have different <code>expects</code> in different promises being picked up correctly by jest</li>
  <li>having always a teardown where the resources are cleaned up even if there are exceptions</li>
  </ul>
  <p>There are 4 function I would like to chain, similar to the Act - Arrange - Assert pattern.
  The problem is that those funktion have async calls to f.e. a database and can have expectation.</p>
  <p>To have a better understanding here is some pseudocode how this could look in my head. This only should provide a better understanding of the issue.</p>
  <pre><code>const setupResources =  () =&gt;  new Promise(resolve =&gt; {
    //async calls to a database to setup a test
    await dynamoDBClient.put(params).promise();
    // returns data which are necessary for the other steps
    resolve(data);
  });
  
  const act = (data) =&gt; new Promise(resolve =&gt; {
    // doing some stuff
    const otherdata = {data};
      // calling some endpoint asyc
     const result = await axios.post(
                `https://restapi.com/test`,
                JSON.stringify(otherdata),
            );
    // some expects
    expect(result.status).toBe(200);  
  
    // again some data which will be needed in the other steps
    resolve({someInformation: 'info', data}) ;
  });
  
  const assertIt = (data) =&gt; {
    const prarams = {/*db related data*/}
    new Promise(resolve=&gt;{
        const dbdata = await dynamoDBClient.get(params).promise();
  
        // some expectation
        expect(dbdata).toBe(data.data);
        resolve();
   })
  
  };
  
  const tearDown = (data) =&gt; {
    // cleanup of data base trash which should always happen
    await dynamoDBClient.delete(data.params).promise();
  };
  </code></pre>
  <p>The actual test should look something along those line if that makes sense.</p>
  <pre><code>it('test',()=&gt;{
  return setupResources()
          .then(act(data))
          .then(assertIt(data))
          .finally(teardown(data));
  })
  </code></pre>