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
- Jest Matchers
- 패캠
- ESLint
- F-Lab 오티
- 패스트캠퍼스
- Reconsiliation
- 프론트엔드
- Prettier
- 인프런 멘토링
- F-Lab
- 주간 회고
- 에프랩
- 마이크로프론트엔드
- 비동기 테스트
- virtual DOM
- 데이터 타입
- F-Lab 회고
- Frontend
- TDD
- 모노레포
- github actions
- 가상돔
- Husky
- CICD
- React
- javascript
- aws s3
- 전역 상태 관리
- context api
- jest
Archives
- Today
- Total
bytrustu tech blog
[Jest] 비동기 테스트 하기 본문
비동기 함수를 테스트해보자.
https://jestjs.io/docs/asynchronous
async.js
fetchProduct 함수의 매개변수가 error의 경우 Error를 발생합니다.
const fetchProduct = (error) => {
if (error === 'error') {
return Promise.reject('네트워크 오류가 발생했습니다.');
}
return Promise.resolve({ id: 1, name: 'bytrustu', price: 1000 });
}
module.exports = fetchProduct;
Promise
test의 callback 함수의 매개변수 첫 번째 값은 완료된 시점을 개발자가 지정할 수 있도록 테스트 가능합니다.
test('Promise 테스트 합니다.', (done) => {
fetchProduct().then((item) => {
expect(item).toEqual(obj);
done();
})
});
비동기 함수의 결과를 return 하게 되면 done 처리가 없더라도 테스트 수행이 가능합니다.
test('비동기 함수의 결과를 return 하여 테스트 합니다.', () => {
return fetchProduct().then((item) => {
expect(item).toEqual(obj);
})
});
Async/Await
test('async await으로 테스트 합니다.', async () => {
const product = await fetchProduct();
expect(product).toEqual(obj);
});
. resolves/. reject
test('resolves 테스트 합니다.', () => {
expect(fetchProduct()).resolves.toEqual(obj);
});
test('resolves 거짓을 테스트 합니다.', () => {
expect(fetchProduct()).resolves.not.toEqual({ ...obj, price: 2000 });
});
test('reject 테스트 합니다.', () => {
expect(fetchProduct('error')).rejects.toBe('네트워크 오류가 발생했습니다.');
});
'TDD' 카테고리의 다른 글
[Jest] Mock (0) | 2022.12.21 |
---|---|
[Jest] Matchers 사용하기 (2) | 2022.12.18 |
[Jest] 초기 환경설정 (0) | 2022.12.17 |
Comments