몽땅뚝딱 개발자
[Mocha] 테스팅 라이브러리 with. 모카(Mocha), chai - (1) 시작 본문

회사에서 스터디를 하게되었다.
테스트는 백엔드에서 더 많이 사용하고 편리함도 높다고 알고있지만 프론트엔드에서도 분명 큰 역할을 하리라 생각한다.
공부기록은 여기...✏️
🔖 이펙티브 소프트웨어 테스팅
∕ 읽는 중
be-a-weapon.tistory.com
◽️ 모카란?
테스트 프레임워크다.
- 노드JS에서 많이 사용된다.
- assertion을 지원하지 않는다. 그래서 노드JS에서 제공하는 assert 모듈을 사용하거나 다른 Assertion 라이브러리(예를 들어 chai)를 설치해서 사용한다.
◽️ 설치하기
노트JS에서 제공하는 assert 모듈을 사용할 수도 있지만 chai는 체이닝 할 수 있는게 장점이라고해서 결정했다.
$ npm i --save-dev mocha
$ npm i chai @types/chai --dev
◽️ 사용하기
책에 있는 예제를 Javascript + Typescript로 변경해보았다.
📄 chapter2.ts
export interface Estimate {
name: string,
point: number
}
/**
* 플래닝 포커 시 가장 높은 점수, 가장 낮은 점수를 준 평가자를 리턴하는 함수
* @param estimateList - 평가자 리스트
* @returns [가장 높은 점수를 준 평가자, 가장 낮은 점수를 준 평가자]
* */
export const planningPoker = (estimateList: ReadonlyArray<Estimate>): ReturnType<()=> [Estimate, Estimate]> => {
let highestScoreEstimate: Estimate = {} as Estimate
let lowestScoreEstimate: Estimate = {} as Estimate
for (const estimate of estimateList) {
if (Object.keys(highestScoreEstimate).length === 0 || estimate.point > highestScoreEstimate.point) {
highestScoreEstimate = estimate
}
if (Object.keys(lowestScoreEstimate).length === 0 || estimate.point < lowestScoreEstimate.point) {
lowestScoreEstimate = estimate
}
}
return [highestScoreEstimate, lowestScoreEstimate]
}
📄 chapter2.spec.ts
import { expect } from 'chai';
import {planningPoker} from "../script/chapter1";
describe('chapter1', () => {
it('multiList test', () => {
expect(planningPoker([
{ name: 'dong', point: 10 },
{ name: 'min', point: 5 },
{ name: 'hwang', point: 3 }
])).to.deep.equal([
{ name: 'dong', point: 10 },
{ name: 'hwang', point: 3 }
])
});
})


출처
마우리시오 아니시, 이펙티브 소프트웨어 테스팅 (제이펍, 2023)
'Development > 환경설정' 카테고리의 다른 글
[Mocha] 테스팅 라이브러리 with. 모카(Mocha), chai - (2) 문법 (0) | 2023.05.21 |
---|---|
Gitmoji를 사용한 commit 쪼개기 (0) | 2023.05.14 |
[IntelliJ] Postfix Completion 예제 및 적용하기 (0) | 2023.03.13 |
[IntelliJ] 저장 시 동작 설정(Actions on Save) (0) | 2023.02.12 |
Mac에서 MySQL 설치하기 (0) | 2022.05.01 |
Comments