mercredi 11 décembre 2019

Jest.js: Error fails to register with await expect(asyncMethod).rejects.toThrow()

My async method under testing appears to throw an Error object as expected, but does not register as a passed test:

✕ exists() throws Error (1ms)

  ● constructor › exists() throws Error

    Filename not found

      64 |       return fileNameMatch![1];
      65 |     }
    > 66 |     else { throw Error("Filename not found"); }
         |                  ^
      67 |   }
      68 | 
      69 |   // todo simplify this a ton to exact match my hosted, unchanging sc url

      at ScriptTagTester.Object.<anonymous>.ScriptTag.getFileName (functions/src/classes/ScriptTag.ts:66:18)
      at ScriptTagTester.Object.<anonymous>.ScriptTagTester.getFileName (functions/src/classes/__tests__/ScriptTag.test.ts:16:29)
      at ScriptTagTester.ScriptTag [as constructor] (functions/src/classes/ScriptTag.ts:36:31)
      at new ScriptTagTester (functions/src/classes/__tests__/ScriptTag.test.ts:12:5)
      at functions/src/classes/__tests__/ScriptTag.test.ts:155:29
      at step (functions/src/classes/__tests__/ScriptTag.test.ts:46:23)
      at Object.next (functions/src/classes/__tests__/ScriptTag.test.ts:27:53)
      at functions/src/classes/__tests__/ScriptTag.test.ts:21:71
      at Object.<anonymous>.__awaiter (functions/src/classes/__tests__/ScriptTag.test.ts:17:12)
      at Object.<anonymous> (functions/src/classes/__tests__/ScriptTag.test.ts:152:33)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 5 passed, 6 total

Here is the code below. I'm making the protected methods public through a testing child class.

Does anyone know why Jest isn't catching the throw? I believe my example below follows the format in the docs when using .rejects on an async function: https://jestjs.io/docs/en/expect.html#rejects

// ScriptTag.test.ts 
  test("exists() throws Error", async() => {
    jest.spyOn(ScriptTagTester.prototype, "fetchAllScriptTags")
      .mockResolvedValueOnce(dummyScriptTagsMissingTarget);
    const scriptTagTester = new ScriptTagTester("","", "");

    await expect(scriptTagTester.exists())
      .rejects
      .toThrow(Error);
      // .toThrow(); // same issue 
      // .toThrow("Filename not found"); // same issue
      // .toThrow(Error("Filename not found")); // same issue
  })
// ScriptTag.ts
  protected getFileName(filePathOrUrl: string): string {
    const fileNameMatch: string[]|null = filePathOrUrl.match(/\/?(\w+.js)$/);
    if (Boolean(fileNameMatch)) {
      // capture group is always assigned to [1], even when full match === capture group
      return fileNameMatch![1];
    }
    else { throw Error("Filename not found"); } // adding new keyword doesn't help
  }

  protected async exists(): Promise<boolean> {
    const scriptTags: ScriptTagObject[] = await this.fetchAllScriptTags();

    const scriptTagFileNames: string[] = scriptTags.map((scriptTagRecord: ScriptTagObject) => {
      const tagFileName: string = this.getFileName(scriptTagRecord.src);
      return tagFileName;
    });

Aucun commentaire:

Enregistrer un commentaire