Created
March 11, 2024 07:55
-
-
Save msbaek/d8e9ac71902e5a2b5d0073a00ad8c240 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { calc, search } from '@/pages/goodsList/search'; | |
describe('search api', () => { | |
it('search api를 호출한다.', async () => { | |
const { data } = await search({ keyword: 'blackpink' }); | |
// console.log(data); | |
expect(data).toMatchSnapshot(); | |
}); | |
}); | |
describe('search api', () => { | |
it('calc returns sum.', () => { | |
const addend = 3; | |
const audend = 4; | |
const sum = calc(addend, audend); | |
expect(sum).toBe(7); | |
}); | |
}); | |
// ----------------------------------------------------------------------------------------------- | |
// 1. 동적 데이터의 구조만 검증하기 | |
// 예상되는 키와 타입 확인 | |
expect(data).toHaveProperty('totalCount'); | |
expect(typeof data.totalCount).toBe('number'); | |
expect(Array.isArray(data[0])).toBe(true); | |
data[0].forEach(item => { | |
expect(item).toHaveProperty('SHOP_NO'); | |
expect(typeof item.SHOP_NO).toBe('number'); | |
// 추가적인 필드 검증... | |
}); | |
// 2. 특정 키의 변동성 있는 값 대응 | |
// 어떤 키의 값이 변할 수 있지만, 그 값의 범위나 조건을 알고 있는 경우, 그 조건에 맞게 검증합니다. 예를 들어, PRICE 값이 항상 양수여야 한다면, 이를 검증할 수 있습니다. | |
data[0].forEach(item => { | |
expect(item.PRICE).toBeGreaterThan(0); | |
// 다른 조건 검증... | |
}); | |
// 3. 부분적인 데이터 검증 | |
// 전체 데이터 대신 일부 중요한 데이터 포인트에 대해서만 검증을 수행합니다. 예를 들어, totalCount와 같이 변하지 않는 값이나, API의 주요 기능과 직결된 특정 값들을 선택하여 검증합니다. | |
expect(data.totalCount).toBeGreaterThanOrEqual(0); // 예상되는 최소값 검증 | |
// 다른 중요 데이터 포인트 검증... | |
// 4. 스냅샷 테스트 | |
// 결과의 정확한 구조를 스냅샷으로 저장하고, 후속 테스트에서 이 스냅샷과 비교하여 변화를 감지합니다. 스냅샷 테스트는 구조가 변경되지 않았는지 검증할 때 유용합니다. 하지만, 동적 데이터에 대해서는 자주 스냅샷을 업데이트해야 할 수 있습니다. | |
expect(data).toMatchSnapshot(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment