Web/JavaScript

[Jest] Testing with Error Return

llHoYall 2020. 12. 28. 21:41

Source Code

Let's create a function to test.

const funcWithError = (): void => {
  throw new Error("My Error")
}

Test Code

Now, let's test our function.

describe("Test With Error", () => {
  it("tests a function with an error return", () => {
    try {
      calculator.funcWithError();
    } catch (err) {
      expect(err).toEqual(new Error("My Error"));
    }
  });
});

I tested with a try...catch statement.

 

Yay~ It works well!!

반응형