일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- multicall
- 머신러닝기초
- 컨트렉트 동일한 함수이름 호출
- 러스트기초
- rust 기초
- ethers websocket
- 오블완
- git rebase
- 러스트 기초
- 스마트컨트렉트테스트
- 스마트컨트렉트 예약어 함수이름 중복
- 체인의정석
- ethers typescript
- SBT표준
- vue기초
- 스마트컨트렉트 함수이름 중복 호출
- 티스토리챌린지
- ethers
- erc4337 contract
- 러스트 기초 학습
- erc4337
- 컨트렉트 배포 자동화
- Vue
- ethers v6
- chainlink 설명
- ethers type
- 계정추상화
- Vue.js
- 스마트 컨트렉트 함수이름 중복
- ambiguous function description
- Today
- Total
체인의정석
Jest에서 error를 발생시켜야 하는 경우 테스트 본문
이제 기능 작동이 끝나고 온갖 에러 상황에서 제대로 작동하는지를 테스트 하기 위해 일부러 틀린 내용을 넣어서 jest를 테스트 하는 단계이다.
Expect · Jest
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" that let you validate different things.
jestjs.io
위의 jest 공식 문서에서 어떤 문법을 써야 하는지 살펴보기로 하였다.
https://jestjs.io/docs/expect#tothrowerror
Expect · Jest
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" that let you validate different things.
jestjs.io
현재 만든 모듈은 조건에 만족하지 않을 시 에러를 리턴해주어야 한다. 따라서 .toThrow(error?)를 사용하기로 하였다.
test('throws on octopus', () => {
expect(() => {
drinkFlavor('octopus');
}).toThrow();
});
위와 같은 상황에서
function drinkFlavor(flavor) {
if (flavor == 'octopus') {
throw new DisgustingFlavorError('yuck, octopus flavor');
}
// Do some other stuff
}
drinkFlaver가 리턴하는 에러가 다음과 같다면, 에러가 발생하고, 테스트는 통과하게 된다.
test('throws on octopus', () => {
function drinkOctopus() {
drinkFlavor('octopus');
}
// Test that the error message says "yuck" somewhere: these are equivalent
expect(drinkOctopus).toThrowError(/yuck/);
expect(drinkOctopus).toThrowError('yuck');
// Test the exact error message
expect(drinkOctopus).toThrowError(/^yuck, octopus flavor$/);
expect(drinkOctopus).toThrowError(new Error('yuck, octopus flavor'));
// Test that we get a DisgustingFlavorError
expect(drinkOctopus).toThrowError(DisgustingFlavorError);
});
위와 같이 특정 에러가 나는지 체크할 수도 있다.
expect(new 클래스명(들어가는api값)).toThrowError(에러타입 또는 메세지);
이렇게 잘못된 클래스 생성시에 에러를 리턴해주는 모듈이므로, 위와 같이 에러를 써주면 된다.
'개발 > backend(js,ts)' 카테고리의 다른 글
security lint) Unsafe Regular Expression, 이메일 정규표현식 검사 (0) | 2021.09.08 |
---|---|
Node.js) package-lock.json 깃허브 버전관리 (0) | 2021.09.08 |
Jest 를 이용하여 파일 하나 테스트 (0) | 2021.09.06 |
eslint plugin security 모듈 (0) | 2021.08.30 |
Toobusy.js에 대하여 (1) | 2021.08.27 |