[3주차] 테스트 코드 작성하기

toto9602·2022년 2월 22일
0

Re: 스터디

목록 보기
3/3

참고자료 : 테스트 코드 작성하기 - jest, typescript

3주차 주제는, 작성해 오고 있는 학점 계산기에 대한 테스트 코드 작성하기!

테스트를 위해 jest, ts-jest 등의 관련 패키지를 먼저 설치해 주었다 :)

gradeCalculator.test.ts

import { SAMPLE_GRADE_ONE, SAMPLE_GRADE_TWO, SAMPLE_GRADE_THREE } from "../mock/index";
import { ERROR_GRADE_GRADE, ERROR_GRADE_CREDIT, ERROR_GRADE_COURSE } from "../mock/index";
import { Grades } from '../week2/calculator/grades';


describe("학점 계산기 Test", () => {
    test("성적 유효성 검사", () => {
        const gradeError = new Grades(ERROR_GRADE_GRADE, 0, 0, 0);
        expect(gradeError.calc).toThrow(Error)
    });

    test("학점 유효성 검사", () => {
        const creditError = new Grades(ERROR_GRADE_CREDIT, 0, 0, 0);
        expect(creditError.calc).toThrow(Error);
    });

    test("과목명 유효성 검사", () => {
        const courseError = new Grades(ERROR_GRADE_COURSE, 0, 0, 0);
        expect(courseError.calc).toThrow(Error);
    });

    test("학점 계산 검사", () => {
        const grades = new Grades(SAMPLE_GRADE_ONE, 0, 0, 0);
        grades.calc();
        expect(grades.getGradeResult()).toBe(4.25);
    })
})

describe를 통해 test를 그룹화해 주고,
그 안에 4가지 테스트를 작성했다!

ERROR_GRADE_GRADE, ERROR_GRADE_CREDIT, ERROR_GRADE_COURSE에는 각각 성적, 학점, 과목명에서 유효하지 않은 값을 입력해 두었다.

원시값 포장을 해 두었기 때문에(?) 계산 과정에서 Error가 발생할 것으로 예상!

4번째 테스트에서는 유효한 값을 바탕으로, 예상한 대로 학점이 잘 계산되는지를 테스트했다.

npm test

테스트 실행 결과로, 작성한 4가지 테스트가 모두 정상적으로 통과되는 것까지 확인 :)

profile
주니어 백엔드 개발자입니다! 조용한 시간에 읽고 쓰는 것을 좋아합니다 :)

0개의 댓글