라이브러리 설치
$ 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/
'프로그래밍 > Nest.js' 카테고리의 다른 글
[ Nest.js ] 테스트 코드 작성 FIRST 원칙 (0) | 2024.06.30 |
---|---|
[ Nest.js ] Logger 구현 (0) | 2024.06.26 |
[ NestJS ] Configuration - .env (0) | 2024.06.04 |
[ NestJS ] build 를 이용하여 배포하는 이유 (0) | 2024.06.02 |
[ NestJS ] Configuration : Schema validation - Joi 예시 (0) | 2024.05.28 |