Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- typescript
- Algorithm
- configfile
- InfluxDB
- modbus
- algorhtim
- 스택/큐
- React
- Two Pointer
- dfs
- DP
- summerwintercoding
- 1844
- 파이썬
- frontend
- queue
- javascirpt
- 코딩테스트
- set활용
- Stack
- 42587
- javascript
- 프로그래머스
- 개발브로그
- pymodbus
- 알고리즘
- data platform
- 완전탐색
- python
- 좌표이동
Archives
- Today
- Total
DM Log
[JEST] 1. JEST 소개 및 간단한 테스팅 본문
- JEST 란?
자바스크립트(타입스크립트)에서 인기 있는 테스트 프레임워크이자 테스트 러너로 목 객체와 코드 커버리지 수집을 갖춘 메타의 오픈소스
- 테스트 작성 및 실행 : 다양한 방식으로 코드 동작을 확인하는 테스트 작성 가능
- 목 객체 지원 : 외부 의존성을 모방하여 테스트
- 코드 커버리지 : 테스트가 코드의 어느 부분을 검증했는지 분석
- 유연하고 직관적인 API : 쉽게 작성 가능한 문법 제공
- JEST 세팅
프로젝트 초기화
node.js 패키지 모듈 의존성 버전 관리를 위해 package.json 파일 생성
npm init -y
Jest 설치와 TypeScript 설치
npm install --save-dev jest ts-jest @types/jest typescript
TypeScript 설정 (선택)
TypeScript 의 설정을 tsconfig.json 파일을 생성하여 입력
npx tsc --init
{
"compilerOptions": {
"target": "ESNext", // 컴파일된 JS 코드의 타겟
"module": "ESNext", // 모듈 시스템 (ES Modules)
"moduleResolution": "Node", // Node.js 방식 모듈 해석
"strict": true, // 엄격 모드
"esModuleInterop": true, // ES 모듈 간 상호 운용성 활성화
"skipLibCheck": true, // 라이브러리 타입 검사 생략
"outDir": "./dist" // 컴파일된 파일의 출력 경로
}
}
Jest 설정 파일 생성 (TypeScript 작업시 필수)
touch jest.config.js
module.exports = {
preset: 'ts-jest', // ts-jest를 사용하여 TypeScript 처리
testEnvironment: 'node', // Node.js 환경에서 실행
};
package.json 스크립트 추가
"scripts": {
"test": "jest"
}
- 간단한 테스팅
src/index.ts
export function add(a: number, b:number) {
return a+b;
}
src/index.test.ts
import {add} from './index';
test("add: 1+2=3", () => {
expect(add(1,2)).toBe(3)
})
테스트 실행
npm test
결과
PASS src/index.test.ts
√ add: 1+2=3 (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.188 s, estimated 3 s
Ran all test suites.