title: Jest: stuck on [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block,
body: <p>I'm trying to load a file and search for specific terms within it. If the file doesn't have any of the terms, then I need to throw an error message. This all appears to work in the code, but when I created my Jest test cases, I started getting the message:</p>
  <blockquote>
  <p>[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). ...</p>
  </blockquote>
  <p>Everything is wrapped in a try/catch block, so the message has no meaning to me and I'm guessing it is probably a problem with what I'm doing in Jest. I'm new to Jest testing so any help would be appreciated. I've isolated the test so, I know this is the one causing issues.</p>
  <p>The test case:</p>
  <pre><code>test('missing functions in file', () =&gt; {
    const input = 'no functions here'
    jest
      .spyOn(fs, 'readFile')
      .mockImplementation(() =&gt;
        Promise.resolve(Buffer.from(input, 'utf-8').toString())
      )
  
    expect(async () =&gt; await loadFunctionsFromFile()).toThrowError(
      `No functions found in file.\nThey must have the format of '/* fn: */function' or '/* fn: */ function'.`
    )
  })
  </code></pre>
  <p>My code being tested:</p>
  <pre><code>export async function loadFunctionsFromFile() {
    try {
      let flieText = await getFnTextFromFile(
        jsonTransformConfig.providedFunctionsFile
      )
      flieText += await getFnTextFromFile(jsonTransformConfig.addOnFunctionsFile)
  
      return flieText
    } catch (err: any) {
      throw errorHandler(err, 'loadFunctionsFromFile')
    }
  }
  
  async function getFnTextFromFile(fileName: string) {
    try {
      const fileText = await tryToGetFileText(fileName)
      const functionsText = tryToRemoveExtraText(fileText)
  
      return functionsText
    } catch (err: any) {
      throw errorHandler(err, 'getFnTextFromFile')
    }
  }
  
  async function tryToGetFileText(fileName: string) {
    try {
      let functionsText = await fs.readFile(fileName, 'utf-8')
  
      return functionsText
    } catch {
      throw new Error(`'${fileName}' not found`)
    }
  }
  
  // export async function getDynamicFunctions() {}
  function tryToRemoveExtraText(functionsText: string) {
    try {
      // clean up special characters from file (CRLF, EoF)
      const cleanText = functionsText.replace(/\r\n/g, '\n').trimEnd()
      const normalizedText = replaceAll(
        cleanText,
        prettierSearchTerm,
        legacySearchTerm
      )
  
      let functionStart = normalizedText.indexOf(legacySearchTerm)
      if (functionStart === -1) {
        throw new Error(
          `No functions found in file.\nThey must have the format of '${legacySearchTerm}' or '${prettierSearchTerm}'`
        )
      }
      return normalizedText.substring(functionStart)
    } catch (err: any) {
      throw errorHandler(err, 'tryToRemoveExtraText')
    }
  }
  </code></pre>
  <p>The errorHandler function just returns an error.</p>