title: Testing AWS Cognito from Jest fails, but the same code in a component works
body: <p>I am trying to integration test my react components.  I need a valid JWT token to call the APIs to get the data to render the tables to test the components and interactions.  It's about a million times easier (in theory) to just do this with a jest test that try to mock the whole of the universe, so I'm not very interested in mocking.  I could use a browser test I suppose, but they always seem to be pretty flaky so I'd rather use a jest integration test.</p>
  <p>Problem is, despite having the same exact code; when I run it as a jest test it fails, and when I run it in a react component, it works.  Same credentials; same code. I keep looking for what context I'm missing somehow; but I'm not seeing anything sticking out.</p>
  <p>I have the following test:</p>
  <pre><code>    it('calls cognito library', async() =&gt; {
          const poolData = {
              UserPoolId: awsconfig.aws_user_pools_id,
              ClientId: awsconfig.aws_user_pools_web_client_id
          };
  
          const userPool = new CognitoUserPool(poolData);
          const authenticationData = {
              Username: &quot;myuserhere@example.com&quot;,
              Password: &quot;mypassword123#&quot;,
          };
          const authenticationDetails = new AuthenticationDetails(
              authenticationData
          );
          const userData = {
              Username: &quot;myuserhere@example.com&quot;,
              Pool: userPool
          };
          console.log(&quot;attempting sign in to &quot;, userData, authenticationData)
          const cognitoUser = new CognitoUser(userData);
          return new Promise&lt;CognitoUserSession&gt;((resolve, reject) =&gt; {
              cognitoUser.authenticateUser(authenticationDetails, {
                  onSuccess: function (result) {
                      resolve(result)
                  },
  
                  newPasswordRequired: () =&gt; {
                      reject(&quot;NEW PASSWORD REQUIRED&quot;);
                  },
  
                  onFailure: function (err) {
                      reject(err);
                  },
              })
          })
      })
  </code></pre>
  <p>and the following JSX component:</p>
  <pre><code>import React, {useEffect} from 'react'
  import {useState} from 'react'
  import awsconfig from &quot;./conf/aws-exports&quot;;
  import {AuthenticationDetails, CognitoUser, CognitoUserPool, CognitoUserSession} from &quot;amazon-cognito-identity-js&quot;;
  
  export const TestMe = () =&gt; {
  
      const [auth, setAuth] = useState&lt;boolean&gt;(false)
  
      const poolData = {
          UserPoolId: awsconfig.aws_user_pools_id, 
          ClientId: awsconfig.aws_user_pools_web_client_id
      };
  
      const userPool = new CognitoUserPool(poolData);
      const authenticationData = {
          Username: &quot;myuserhere@example.com&quot;,
          Password: &quot;password123#&quot;,
      };
      const authenticationDetails = new AuthenticationDetails(
          authenticationData
      );
      const userData = {
          Username: &quot;myuserhere@example.com&quot;,
          Pool: userPool
      };
      const cognitoUser = new CognitoUser(userData);
  
      useEffect(() =&gt; {
          console.log(&quot;attempting sign in to &quot;, userData, authenticationData)
          new Promise&lt;CognitoUserSession&gt;((resolve, reject) =&gt; {
              cognitoUser.authenticateUser(authenticationDetails, {
                  onSuccess: function (result) {
                      resolve(result)
                  },
  
                  newPasswordRequired: () =&gt; {
                      reject(&quot;NEW PASSWORD REQUIRED&quot;);
                  },
  
                  onFailure: function (err) {
                      reject(err);
                  },
              })
          }).then(() =&gt; {
              setAuth(true)
          })
      }, [])
  
      return (
          &lt;div&gt;{auth ? &quot;Yes&quot; : &quot;No&quot;}&lt;/div&gt;
      )
  }
  </code></pre>
  <p>and the following index.tsx</p>
  <pre><code>import React from 'react';
  import ReactDOM from 'react-dom';
  import { BrowserRouter as Router } from 'react-router-dom';
  import 'bootstrap/dist/css/bootstrap.css';
  import './index.css';
  import App from './App';
  import * as serviceWorker from './serviceWorker';
  import {TestMe} from &quot;./TestMe&quot;;
  
  ReactDOM.render(
      &lt;Router&gt;
          &lt;TestMe /&gt;
      &lt;/Router&gt;
      , document.getElementById('root'));
  
  serviceWorker.unregister();
  </code></pre>
  <p>I was originally using amplify, but had so many problems, I chucked it out and kept only the config file.  How can I make the jest test work and authenticate?</p>