title: Jest: Share Variables Between Test Files
body: <p>I'll start with an example of how I set up my tests for a backend server. <strong>TL;DR at bottom</strong>.</p>
  <p>This file represents my server:</p>
  <pre class="lang-js prettyprint-override"><code>//server.js
  const express = require('express');
  
  class BackendServer {
      constructor(backingFileDestination, database) {
          this.server = express();
          /* Set up server ... */
          /* Set up a mysql connection ... */
      }
  
      closeMySQLConnectionPool = () =&gt; {
          /* Close the mysql connection ... */
      };
  }
  
  module.exports = BackendServer;
  </code></pre>
  <p>In my <code>package.json</code>, I have the following:</p>
  <pre class="lang-json prettyprint-override"><code>&quot;jest&quot;: {
    &quot;testEnvironment&quot;: &quot;node&quot;,
    &quot;setupFilesAfterEnv&quot;: [
      &quot;&lt;rootDir&gt;/tests/setupTests.js&quot;
    ]
  }
  </code></pre>
  <p>Which allows me to use this setup file for my tests:</p>
  <pre class="lang-js prettyprint-override"><code>//setupTests.js
  const { endpointNames } = require('../../src/common/endpointNames.js');
  const BackendServer = require('../server.js');
  const server = new BackendServer(
      '../backing_files_test/',
      'test',
  );
  const supertester = require('supertest')(server.server);
  
  // drop, recreate, and populate the database once before any tests run
  beforeAll(async () =&gt; {
      await supertester.post(endpointNames.DROP_ALL_TABLES);
      await supertester.post(endpointNames.CREATE_ALL_TABLES);
      await supertester.post(endpointNames.POPULATE_ALL_TABLES);
  });
  
  // clean up the local setupTests server instance after all the tests are done
  afterAll(async () =&gt; {
      await server.closeMySQLConnectionPool();
  });
  </code></pre>
  <p>Notice how I had to import the <code>BackendServer</code> class and instantiate it, then use that instance of it.</p>
  <p>Now, I have other test files, for example <code>test1.test.js</code>:</p>
  <pre class="lang-js prettyprint-override"><code>//test1.test.js
  const { endpointNames } = require('../../src/common/endpointNames.js');
  const BackendServer = require('../server.js');
  const server = new BackendServer(
      '../backing_files_test/',
      'test',
  );
  const supertester = require('supertest')(server.server);
  
  // clean up the local server instance after all tests are done
  afterAll(async () =&gt; {
      await server.closeMySQLConnectionPool();
  });
  
  test('blah blah', () =&gt; {
      /* Some test ... */
  });
  </code></pre>
  <p>The problem is when I go to write <code>test2.test.js</code>, it will be the same as <code>test1.test.js</code>. This is the issue. For every test file, I need to instantiate a new server and then have a separate <code>afterAll()</code> call that cleans up that server's SQL connection. I can't stick that <code>afterAll()</code> inside of <code>setupTests.js</code> because it needs to operate on <code>test1.test.js</code>'s local <code>server</code> instance.</p>
  <p><strong>TL;DR</strong>: Each of my test files instantiates a new instance of my server. What I want is to instantiate the server once in <code>setupTests.js</code> and then simply use that instance in all my tests. Is there a good way to share this single instance between all my test files?</p>