๊ฐ๋ฐ์ ์งํํ๋ฉด์ test๋ฅผ ํ๋ค๋ ๊ฒ์ ์ฌ๋ฌ๊ฐ์ง ์๋ฏธ๋ฅผ ๊ฐ์ง๋ค. ์ฌ์ค ๊ฐ๋ฐ์ ์งํํ๋ ๋ชจ๋ ์ผ๋ จ์ ํ๋ก์ธ์ค๊ฐ test์ด๋ค. ํ์ง๋ง ์ด๋ฌํ test๋ฅผ ์ข๋ ์ธ๋ถํ์ํค๊ณ , ์๋๋๋ก ์ ์๋ํ๋์ง ํ๋ฒ๋ ๊ฒ์ฆํ๋ ๋จ๊ณ๊ฐ ํ์ํ๋ค. ํนํ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ๊ฒ์ด ์๋ ๊ธฐ์กด์ code๋ฅผ ์์ ํ๊ฒ ๋๋ ๊ฒฝ์ฐ(์๋ฅผ ๋ค์ด ๋ฆฌํฉํ ๋ง์ ํ๋๊ฒฝ์ฐ)์๋ ๊ทธ ๊ธฐ๋ฅ์ ๊ฒฐ๊ณผ์๋ ์ํฅ์ ๋ฏธ์น์ง ์์์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด๋์๋ test ๋จ๊ณ์์ ์๋ชป๋ ๋ถ๋ถ์ catchํ ์ ์๋ค.
test๋ฅผ ๊ทธ ๋ชฉ์ ์ ๋ฐ๋ผ ๊ตฌ๋ถ์ง์ผ๋ฉด ๋ค์์ ์ธ๊ฐ์ง ๋ถ๋ฅ๋ก ๋๋ ์ ์๋ค.(๋ค์์ ๋ถ๋ฅ๋ ๋ชจ๋ test์ ๋ํ ์๊ฒฉํ rule์ด ์๋๊ณ , ๋ค์ํ ์ํฉ์ด๋ ๋ชฉ์ ์ ์ํด ๋ ์ธ๋ถํ๋๊ธฐ๋ ๋จ์ํ๋๊ธฐ๋ ํ๋ค)
- Unit Testing (์ ๋ ํ
์คํธ)
code๊ฐ ๊ฐ๋ฐ์์ ์๋๋๋ก ์๋ํ๋์ง ํ์ธํ๊ธฐ ์ํด ๊ฐ๋ณ ์ปดํฌ๋ํธ ๋๋ ๋ชจ๋์ ๋
๋ฆฝ์ ์ผ๋ก ํ์ธํ๋ ๊ฒ
- Integration Testing (ํตํฉ ํ
์คํธ)
์ปดํฌ๋ํธ ๋๋ ๋ชจ๋์ด ๊ทธ๋ฃนํ๋์ ํตํฉ/๊ฒฐํฉ ๋ ๋ ์ ์๋ํ๋์ง ํ์ธํ๋ ๊ฒ
- Functional Testing (๊ธฐ๋ฅ ํ
์คํธ)
์๋น์ค์ ๊ธฐ๋ฅ์ ํ
์คํธํ์ฌ ์ฌ๋ฐ๋ฅธ ์์
์ ์ํํ๋์ง ํ์ธํ๋ ๊ฒ
javascript๋ก ์์ฑ๋ ํ๋ก์ ํธ์ test code๋ฅผ ์์ฑํ ๋์ ์ฌ์ฉํ ์ ์๋ ๊ฒ๋ค์ ๋ค์ํ๋ค. ๋ํ์ ์ผ๋ก unit test๋ฅผ ์์ฑํ ๋ jest
, mocha
, chai
๋ฑ์ด ์๋ค. ์ด๋ค์ ๋ค ๋น์ทํ ๋ชฉ์ ์ ๊ฐ์ง๊ณ ์๊ณ ๋ฎ์์์ง๋ง ๋ค๋ฅธ ํน์ฑ๋ค์ ๊ฐ์ง๋ค.
๋๋ ๋๋ถ๋ถ์ test code๋ฅผ ์์ฑํ ๋, react
๋ก ์ง์ฌ์ง ์ปดํฌ๋ํธ๋ฅผ jest
๊ธฐ๋ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝํ์ด ์๋ค.
์ฌ์ฉํ๋ testing library๋ enzyme, react-testing-library ์ด ๋ ๊ฐ์ง ์ด๋ค.
ํ์ง๋ง ์ ๋๊ฐ์ง๋ ์ด๋ ค์ด์ ์ด ์ด๋ค ๋ถ๋ถ์ด ์ด๋ค ๊ณผ์ ์ผ๋ก testing ๋๋์ง ์ก์์ผ๋ก ํ์ธํ๊ธฐ ์ด๋ ต๋ค๋ ๊ฒ์ด๋ค. snapshot
๊ธฐ๋ฅ์ ์ด์ฉํด์ ์คํ์ผ๋ง ์ฝ๋๋ฅผ ํ์ธํ ์ ์์ง๋ง ์ค์ ๋ก ์ด๋ค ๊ณผ์ ์ ๊ฑฐ์น๋์ง ๋ง์ด๋ค. ๊ทธ๋์ ํ๋ฒ ๊ฒํ ํด๋ณธ ๊ฒ์ด e2e testing tool์ค์ ํ ๊ฐ์ง์ธ cypress
์ด๋ค.
Cypress๋ e2e test ๋ฟ๋ง์๋๋ผ unit test, integration test๊น์ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค. ์ข๋ scenario
/user-oriented
์ ์ฑ๊ฒฉ์ ๊ฐ์ง๊ณ ์๋ tool์ด๋ค.
cypress
๋ฅผ ์ ํ๋ฉด์ ๋๊ผ๋ ๋ช๊ฐ์ง ์ฅ์ ์ค ํ๋๋ ๋ฐ๋ก GUI์ด๋ค. ๋ด๊ฐ ์ด๋ค์์ผ๋ก test๋ฅผ ํ๊ณ ์๊ณ , test failure๊ฐ ๋ฐ์ํ์ ๊ฒฝ์ฐ์๋ ์ด๋์์ ๋ฐ์ํ๋์ง log์ ํจ๊ป ํ์๋๋ค.

sample scenario
๊ฐ ๊ธฐ๋ณธ์ผ๋ก ํฌํจ๋์ด ์๊ธฐ ๋๋ฌธ์ code ์์ฑ์ ๋ ํผ๋ฐ์ค๋ก ์ฐธ์กฐ๊ฐ ๊ฐ๋ฅํ๋ค.
๋ ์ ์ฉํ method๋ฅผ ์ ๊ณตํด์ฃผ์ด ํธ์์ฑ์ด ๋๋ค.
cy.viewport(550, 750) // Set viewport to 550px x 750px
cy.getCookie('auth_key') // Get cookie with name 'auth_key'
cy.go('back') // Go back
react-testing-library
๋ฅผ ์ด์ฉํด์ test code๋ฅผ ์์ฑํ ๋์๋ ํญ์ custom hook์ด๋ store, ๋น๋๊ธฐ ์์ฒญ ๋ฑ์ Mocking ํด์ฃผ์ด์ผ ํ๊ณ , ๋ก์ง์ด ํฌ๊ฒ ๋ณ๊ฒฝ๋๋ฉด test code ๋ํ ๋ง์ ๋ถ๋ถ์ ์์ ํด์ผํ๋ ๋จ์ ์ด ์์๋ค.
cypress
๋ฅผ ์ฌ์ฉํ์ ๋์๋ local
์์ ํด๋น service๋ฅผ ์คํํ ๋ค ๊ทธ์ ๋ํด ๋ด๊ฐ ์์ฑํด๋์ test code๋๋ก ๋์์ด ๋๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฐ ๋ฒ๊ฑฐ๋ก์์ด ์ค์๋ค.
ํ์ง๋ง, cypress
์๋ ๋ช๊ฐ์ง ๋จ์ ์ ๋ฐ๊ฒฌํ๋๋ฐ ์ผ๋จ ์์ฑ๋๊ฐ ๋ฎ๋ค๋๊ฒ, ์๋ฌ ํธ๋ค๋ง์ด ์ฝ์ง ์๋ค๋๊ฒ, ๊ทธ๋ฆฌ๊ณ test code๋ฅผ ๋ค ๋ง์น ๋๊น์ง ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด jest
๋ณด๋ค ๊ธธ์๋ค. ์ถ๊ฐ๋ก async
/await
์ฌ์ฉ๋ฐฉ๋ฒ์ด ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ๊ณผ ๋ค๋ฅด๋ค๋๊ฒ๋..
cypress
๋ฅผ ์์๋ณด๋ฉด์ ์ฌ๋ฌ ์ฅ์ ๋ ์์ง๋ง, ๋จ์ ๋ ๋ช
ํํ๊ธฐ ๋๋ฌธ์ ๋ฑํ "์ด๊ฒ์ rtl
๋ณด๋ค ์ข์ testing tool์ด๋ค" ๋ผ๊ณ ํ์ธ์ ํ์ง๋ ๋ชปํ๊ฒ ๋ค. ์ด์ฉ๋ฉด ์ง๊ธ ์ฌ์ฉํ๋ rtl
์ ์ฌ์ฉํด ์๋ฏธ์๊ฒ test code๋ฅผ ์์ฑํ๋ ๊ฒ๋ ์์ฐ์ ์ธ test๋ฅผ ํ ์ ์๋ ๋ฐฉ๋ฒ ์ค ํ๋์ผ ๊ฒ์ด๋ค.
๋ชฉ์ ์ ๋ง์ถ์ด ์ ์ฌ์ฉํด test์ ๋ค์ด๋ ์๊ณ ๋ฅผ ๋์.
์ฐธ์กฐ
https://medium.com/welldone-software/an-overview-of-javascript-testing-in-2019-264e19514d0a
https://docs.cypress.io/