반응형
jest는 프론트엔드에서 자주 사용되는 테스팅 라이브러리 입니다. 간단한 유닛 테스트부터 통합 테스트, E2E 테스트까지 가능합니다.
Common Matchers
toBe
같은 메모리 주소를 가리키고 있는지 확인
test('two plus two is four', () => {
expect(2 + 2).toBe(4);
});
toEqual
값 자체가 같은지 비교
test('object assignment', () => {
const data = {one: 1};
data['two'] = 2;
expect(data).toEqual({one: 1, two: 2});
});
Truthiness
test('null', () => {
const n = null;
expect(n).toBeNull();
expect(n).toBeDefined();
expect(n).not.toBeUndefined();
expect(n).not.toBeTruthy();
expect(n).toBeFalsy();
});
test('zero', () => {
const z = 0;
expect(z).not.toBeNull();
expect(z).toBeDefined();
expect(z).not.toBeUndefined();
expect(z).not.toBeTruthy();
expect(z).toBeFalsy();
});
toBeNull
null 인지
toBeUndefined
undefined 인지
toBeDefined
toBeDefined의 반대
toBeTruthy
true 인지
toBeFalsy
false 인지
Numbers
test('two plus two', () => {
const value = 2 + 2;
expect(value).toBeGreaterThan(3);
expect(value).toBeGreaterThanOrEqual(3.5);
expect(value).toBeLessThan(5);
expect(value).toBeLessThanOrEqual(4.5);
})
toBeGreaterThan
초과
toBeGreaterThanOrEqual
이상
toBeLessThan
미만
toBeLessThanOrEqual
이하
Strings
toMatch
정규표현식으로 검사
test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});
Arrays and iterables
toContain
배열이나 반복 가능 항목에 해당 값을 포함하는지 체크
const shoppingList = [
'diapers',
'kleenex',
'trash bags',
'paper towels',
'milk',
];
test('the shopping list has milk on it', () => {
expect(shoppingList).toContain('milk');
expect(new Set(shoppingList)).toContain('milk');
});
Exceptions
toThrow
특정 함수가 호출될 때 오류가 발생하는지 테스트
반응형
'🟨JavaScript' 카테고리의 다른 글
[JavaScript] navigator.vibrate로 모바일 웹뷰 진동 기능 추가하기 (1) | 2023.01.29 |
---|---|
[JavaScript] for, forEach(), for ... in, for ... of 반복문 차이 (0) | 2022.03.11 |
[JavaScript] Node.js와 MySQL 연동하기 (0) | 2022.03.08 |
[JavaScript] 객체(Object) 속성(property) 개수 구하는 방법 (0) | 2021.11.25 |