title: How to share data between beforeAll / beforeEach and tests in Jest?
body: <p>We use jest to test our API and have quite complex scenarios. We use the <code>beforeAll</code> functions to set up general helper variables for each test and sometimes to set up tenant separation, in other cases we use the <code>beforeEach</code> functions to set up tenant separation for the tests, with some default configuration for the test tenant, ...</p>
  
  <p>For instance a test could like something like this (as you can see, we use TypeScript to write the tests, in case that would matter):</p>
  
  <pre><code>let apiClient: ApiClient;
  let tenantId: string;
  
  beforeAll(async () =&gt; {
      apiClient = await getClientWithCredentials();
  });
  
  beforeEach(async () =&gt; {
      tenantId = await createNewTestTenant();
  });
  
  describe('describing complex test scenario', () =&gt; {
      it('should have some initial state', async () =&gt; {
          await checkState(tenantId);
      });
  
      it('should have some state after performing op1', async () =&gt;{
          await op1(tenantId);
          await checkStateAfterOp1(tenantId);
      });
  
      it('should have some state after performing op2', async () =&gt;{
          await op2(tenantId);
          await checkStateAfterOp2(tenantId);
      });
  
      it('should have some state after performing op1 and op2', async () =&gt;{
          await op1(tenantId);
          await op2(tenantId);
          await checkStateAfterOp1AndOp2(tenantId);
      });
  
      it('the order of op1 and op2 should not matter', async () =&gt;{
          await op2(tenantId);
          await op1(tenantId);
          await checkStateAfterOp1AndOp2(tenantId);
      });    
  });
  
  describe('another similar complex scenario', () =&gt; {
      // ... you see where this is going
  });
  </code></pre>
  
  <p>The problem is, what's the best way to share those variables initialized by <code>beforeAll</code> and <code>beforeEach</code>? - the above test works if executed with the <code>--runInBand</code> option, which <em>"... runs all tests serially in the current process ..."</em></p>
  
  <p>But it starts failing pretty randomly, when executed in parallel, mostly referring to <code>tenantId</code> being undefined. Given that those tests are part of ~200 similar tests, serially all pass. In parallel it depends on the machine. The build agent which has 8 cores / 16 threads has only 50-60% passing tests. My colleagues with quad core CPUs have ~80% passing tests and for me with dual core CPU sometimes only 1-2 tests fail, other times ~10. So obviously it depends on the amount of parallelism.</p>
  
  <p>I've found 2 GitHub issues where people were mentioning the possibility to use <code>this</code> to share the context (which doesn't work anymore) or to encapsulate everything in <code>describe</code>:</p>
  
  <ul>
  <li><a href="https://github.com/facebook/jest/issues/3553" rel="noreferrer">https://github.com/facebook/jest/issues/3553</a></li>
  <li><a href="https://github.com/facebook/jest/issues/3673" rel="noreferrer">https://github.com/facebook/jest/issues/3673</a></li>
  </ul>
  
  <p>So I tried a very naive approach:</p>
  
  <pre><code>describe('tests', () =&gt; {
      let apiClient: ApiClient;
      let tenantId: string;
  
      beforeAll(async () =&gt; {
          apiClient = await getClientWithCredentials();
      });
  
      beforeEach(async () =&gt; {
          tenantId = await createNewTestTenant();
      });
  
      describe('describing complex test scenario', () =&gt; {
          it('should have some initial state', async () =&gt; {
              await checkState(tenantId);
          });
  
          it('should have some state after performing op1', async () =&gt;{
              await op1(tenantId);
              await checkStateAfterOp1(tenantId);
          });
  
          it('should have some state after performing op2', async () =&gt;{
              await op2(tenantId);
              await checkStateAfterOp2(tenantId);
          });
  
          it('should have some state after performing op1 and op2', async () =&gt;{
              await op1(tenantId);
              await op2(tenantId);
              await checkStateAfterOp1AndOp2(tenantId);
          });
  
          it('the order of op1 and op2 should not matter', async () =&gt;{
              await op2(tenantId);
              await op1(tenantId);
              await checkStateAfterOp1AndOp2(tenantId);
          });    
      });
  
      describe('another similar complex scenario', () =&gt; {
          // ... you see where this is going
      });
  });
  </code></pre>
  
  <p>But this didn't seem to have any effect.
  I'd really like to run the test in parallel, but I couldn't find anything regarding that in the documentation. Maybe I don't know what I should be looking for?</p>