모든 일에 의문을 제기하고 그 의문을 해결하기 위해 지식을 구하고 공부
React-Testing-Library에서 목업 활용법(useSelector 등을 Mock) 본문
React-Testing-Library에서 목업 활용법(useSelector 등을 Mock)
GROWNFRESH 2021. 1. 10. 15:52useSelector, useDispatch 등의 함수는 가짜 데이터로 Mock-up 해야 한다.
stackoverflow.com/questions/60011473/testing-react-redux-useselector
testing react-redux useSelector
I have a react application which uses react-redux, useSelector to get data from the store. The react component has function PersonDataView() { const name= useSelector(state => state.data.na...
stackoverflow.com
위 게시글의 질문자에 따르면,
react testing library에서 목업데이터를 만들 수 있는 방법은 2가지다.
1) redux-mock-store 활용
2) jest 함수 사용
위 게시글에 jest 함수를 활용하는 다양한 방법이 나와 있으니 참고하기 바란다.
1) redux-mock-store 관련 사이트
github.com/vercel/next.js/issues/8145
Hooks Error: Combining with-redux and with-jest/with-jest-react-testing-library · Issue #8145 · vercel/next.js
Bug report Describe the bug Seeing(when using useDispatch hook): FAIL tests/index.test.js: Invariant Violation: Could not find react-redux context value; please ensure the component is wrapped in a...
github.com
2) jest mock함수 관련 사이트
useSelector 함수를 테스트 코드에서 사용하는 방법
먼저 가짜 함수들을 react-redux.js 파일에 export 해준다테스트 케이스에서 아래와같이 jest.mock('react-redux')를 사용하면 위에서 만든 react-redux파일을 찾아 가져온다.다음으로 useSelector mock 함수를 조작
velog.io
// jest로 mocking하는 법 - 간략한 설명과 일련의 순서
www.daleseo.com/jest-fn-spy-on/
[Jest] jest.fn(), jest.spyOn() 함수 모킹
Engineering Blog by Dale Seo
www.daleseo.com
// jest.fn() 등에 대한 상세 설명, 개념을 잡기에 좋음
www.daleseo.com/jest-mock-modules/
[Jest] jest.mock() 모듈 모킹
Engineering Blog by Dale Seo
www.daleseo.com
'테스팅 라이브러리 > React-Testing-Library' 카테고리의 다른 글
에러 | ReferenceError: regeneratorRuntime is not defined (0) | 2021.03.13 |
---|---|
r-t-l는 describe, it을 쓰는가? test를 쓰는가? (0) | 2021.03.13 |
RTL Varient, Queries 종류 + jest Matcher (0) | 2021.02.23 |
TDD의 장점/핵심 과정/RTL의 장점 (0) | 2021.02.23 |
React Testing Library 설치 및 설정 (0) | 2021.01.24 |