title: Async setup of environment with Jest
body: <p>Before running e2e tests in <em>Jest</em> I need to get an authentication token from the server.</p>
  
  <p>Is it possible to do this globally one and set it somehow to the global environment / context for each test ?</p>
  
  <p>I tried it with <code>globalSetup</code> config option:</p>
  
  <pre><code>const auth = require('./src/auth')
  const ctx = require('./src/context')
  
  module.exports = () =&gt; {
      return new Promise(res =&gt; {
          auth.getToken()
              .then(token =&gt; {
                  ctx.init({token})
                  global.token = token
                  res()
              })
  
      })
  }
  </code></pre>
  
  <p><strong>context.js</strong></p>
  
  <pre><code>let _token
  
  const init = ({token}) =&gt; {
      _token = token
  }
  
  const getToken = () =&gt; {
      return _token
  }
  
  module.exports = {
      init,
      getToken
  
  }
  </code></pre>
  
  <p>But both <code>global.token</code> nor <code>ctx.getToken()</code> return undefined.</p>
  
  <p>I need to use helper script and to pass token as env var which is then set as <code>global</code>.</p>
  
  <pre><code>  "scripts": {
      "test": "TOKEN=$(node src/get-token.js) jest"
    },
  </code></pre>
  
  <p>Is there a better approach (which does not involve shell) ?</p>