title: Expect a function throw for synchronous code inside asynchronous function using Jest with Create React App (CRA)
body: <p>I am using Jest for testing with Create React App and I am trying to test an asynchronous function along with a synchronous code and I need it to throw when the synchronous part has an error.</p>
  
  <p>The test consist on expecting the function to throw when recieving wrong arguments types.</p>
  
  <p>The function throw <strong>"Invalid arguments."</strong> when receiving arguments type other than <strong>"undefined"</strong> (function without arguments) or a <strong>"number"</strong>.</p>
  
  <p>The USER_API is the API url to invoke.</p>
  
  <p>Here is the function:</p>
  
  <pre><code>export const getUsers = async (count, ...rest) =&gt; {
    if (["undefined", "number"].includes(typeof count) &amp;&amp; rest.length === 0) {
      const response = await fetch(USERS_API);
      const users = await response.json();
      if (count &amp;&amp; typeof count === "number") {
        return users.slice(0, count - 1);
      }
      return users;
    }
    throw "Invalid arguments.";
  };
  </code></pre>
  
  <p>Here is the test:</p>
  
  <pre><code>it.only("should throw on invalid arguments", () =&gt; {
    const str = "hello";
    expect(() =&gt; getUsers(str)).toThrow(/invalid/gi);
  });
  </code></pre>
  
  <p>I expexted the function to throw</p>
  
  <p>But running the test shows:
  Expected the function to throw an error matching: /invalid/gi But it didn't throw anything.</p>
  
  <hr>
  
  <p>Is the testing method right or am I writing a bad test? If is it bad how can I improve it?</p>
  
  <p>Thank you.</p>