jest - 에러 테스트

raejun·2022년 3월 29일
0

에러 테스트 예제

  add(num) {
    const sum = this.value + num;
    if (sum > 100) {
      throw new Error('Value can not be greater than 100');
    }
    this.value = sum;
  }
 it('add should throw an error if value is greater than 100', () => {
    expect(() => {
      cal.add(101);
    }).toThrow('Value can not be greater than 100');
  });
  • add함수에서 sum이 100을 넘을 경우 예외 처리에 대한 테스트
  • expect에 콜백함수로 에러 상황을 입력한 후 toThrow를 통해 에러 메시지를 확인할 수 있음
profile
정리노트

0개의 댓글