라이브러리 설치

$ npm init
$ npm i -D jest ts-jest @jest/globals @types/jest

 

ts-jest config 파일 생성

$ npx ts-jest config:init

 

1.  간단한 테스트 및 셋팅

테스트 작성할 파일 생성 :  "xxx.spec.ts" 혹은 "xxx.test.ts" 로 생성

      app.spec.ts 에 아래코드를 작성

function sum (a: number, b: number): number {
    return a + b;
}

test(`1 + 2 는 3이다.`, ()=>{
    expect(sum(1, 2)).toBe(3);
})

      

      package.json 에 아래 설정 추가

{
  ...
  "scripts": {
    "test": "jest"
  }
  ...
}

 

      터미널에서 테스트 실행

 $ npm test
 
 PASS  ./app.spec.ts
  ✓ 1 + 2 는 3이다. (1 ms)

 

참고자료

https://kulshekhar.github.io/ts-jest/docs/getting-started/installation/

 

+ Recent posts