React Testing Pattern

์„ค์ •๊ณผ ํ•ด์ง€๋ฅผ ํ†ตํ•œ ํ™˜๊ฒฝ์˜ ๊ฒฉ๋ฆฌ

์œ ๋‹›ํ…Œ์ŠคํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•  ๋•Œ ๊ฐ ํ…Œ์ŠคํŠธ๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๊ณ  ์™ธ๋ถ€ ํ™˜๊ฒฝ์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๊ณ  ํ•ญ์ƒ ๋™์ผํ•œ ํ™˜๊ฒฝ์—์„œ ์ˆœ์ˆ˜ํ•œ ๋ชจ๋“ˆ์˜ ๋™์ž‘๋งŒ์„ ๊ฒ€์ฆํ•ด์•ผ ํ•œ๋‹ค.

์„ค์ •(Setup)

ํ…Œ์ŠคํŠธ๊ฐ€ ์‹œ์ž‘๋˜๊ธฐ ์ „์— ์ˆ˜ํ–‰๋˜๋ฉฐ ๋™์ผํ•œ ํ™˜๊ฒฝ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜ ์„ค์ • ๋ฐ mock์„ ์ฃผ์ž…ํ•œ๋‹ค.

  • jest์—์„œ ์ œ๊ณตํ•˜๋Š” beforeEach beforeAll ๋“ฑ์˜ ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•ด์„œ ๋ฏธ๋ฆฌ ์„ค์ •ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’๋“ค์„ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ๋ชจํ‚นํ•œ๋‹ค.

  • beforeAll() โ†’ ๋™์ผํ•œ ์Šค์ฝ”ํ”„ ๋‚ด(์ž์‹ํฌํ•จ) ํ…Œ์ŠคํŠธ๋“ค์ด ์‹œ์ž‘๋˜๊ธฐ ์ „ ๊ฐ€์žฅ ๋จผ์ € ํ•œ๋ฒˆ ์ˆ˜ํ–‰

  • beforeEach() โ†’ ๋™์ผ ์Šค์ฝ”ํ”„ ๋‚ด(์ž์‹ํฌํ•จ) ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ด์ „์— ์ˆ˜ํ–‰

ํ•ด์ง€(Teardown)

ํ…Œ์ŠคํŠธ์˜ ์ข…๋ฃŒ ํ›„ ๋งˆ๋ฌด๋ฆฌ, ์ดํ›„ ํ…Œ์ŠคํŠธ์— ์˜ํ–ฅ์ด ์—†๋„๋ก ์„ธํŒ…

  • AfterEach() โ†’ ๋™์ผ ์Šค์ฝ”ํ”„ ๋‚ด(์ž์‹ํฌํ•จ) ๊ฐ๊ฐ์˜ ํ…Œ์ŠคํŠธ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋œ ์ดํ›„ ์ˆ˜ํ–‰

  • AfterAll() โ†’ ๋™์ผํ•œ ์Šค์ฝ”ํ”„ ๋‚ด(์ž์‹ํฌํ•จ) ํ…Œ์ŠคํŠธ๋“ค์ด ๋ชจ๋‘ ์ˆ˜ํ–‰๋œ ํ›„ ๋งˆ์ง€๋ง‰์— ํ•œ๋ฒˆ ์ˆ˜ํ–‰

parent Scope ๋จผ์ € ์ˆ˜ํ–‰๋˜๊ณ  ๊ทธ ๋‹ค์Œ child Scope๊ฐ€ ๋‚˜์ค‘์— ์ˆ˜ํ–‰๋œ๋‹ค.

React Testing Library ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ด๋ฏธ ์„ค์ •/ํ•ด์ง€๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์–ด ์ผ๋ฐ˜์ ์œผ๋กœ ๋ณ„๋„์˜ ์„ค์ •์„ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ ํ…Œ์ŠคํŠธ์˜ ๋…๋ฆฝ์  ์ˆ˜ํ–‰ ๋ณด์žฅ์„ ์œ„ํ•œ ๊ฐœ๋…์„ ์ดํ•ดํ•ด์•ผ ํ•˜๊ณ  ๋ณ„๋„๋กœ ์„ค์ •/ํ•ด์ง€๊ฐ€ ํ•„์š”ํ•œ ์ฝ”๋“œ๊ฐ€ ์กด์žฌ

Timer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

  • setTimeout, setInterval, clearTimeout, clearInterval

  • Timer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช‡์ดˆ ๋’ค์— ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋“ฑ์˜ ์ฝ”๋“œ๋“ค์€ ํ…Œ์ŠคํŠธ๊ฐ€ Timer๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•˜๋ฉฐ ์ด๋กœ ์ธํ•ด ํ…Œ์ŠคํŠธ๋Š” ๋Š๋ ค์ง€๊ณ  ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

  • ์ด๋Ÿฐ ๊ฒฝ์šฐ jest์—์„œ ์ œ๊ณตํ•˜๋Š” fake timer ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

beforeEach(() => {
  // timer๋ฅผ mocking
  jest.useFakeTimers()
})
afterEach(() => {
  jest.runOnlyPendingTimers(); // ์ˆ˜ํ–‰๋˜์ง€ ์•Š์•˜๋˜ ๋ชจ๋“  ํƒ€์ด๋จธ ์ˆ˜ํ–‰ 
  jest.useRealTimers(); // ์‹ค์ œ ํƒ€์ด๋จธ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ๋” timer mocking ์ œ๊ฑฐ 
})

์ธํ„ฐ๋ ‰์…˜ ๋‹จ์œ„์˜ ๊ฒ€์ฆ

AAA ํŒจํ„ด (Arrange, Act, Assert)

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋Š” 3๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด ํ•ด๋‹น ํ…Œ์ŠคํŠธ๊ฐ€ ๋ฌด์—‡์„ ํ•˜๊ณ  ์–ด๋–ค ๋ชฉ์ ์„ ๊ฐ–๋Š”์ง€ ๋ณด๋‹ค ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑ

  • Arrange (์ค€๋น„)

    • ํ…Œ์ŠคํŠธ ์‹คํ–‰ ์ค‘ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ค€๋น„

    • BeforeEach, afterEach

    • Render Components

    • Mocking, State, Service ๋“ฑ

  • Act (์‹คํ–‰)

    • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์‹คํ–‰

    • User์˜ Action (Click, State change ๋“ฑ)

    • RTL์€ act() wrapping ๋ถˆํ•„์š”

  • Assert (๋‹จ์–ธ)

    • ์‹คํ–‰ํ•œ ์ฝ”๋“œ ๊ฒฐ๊ณผ์˜ ๋™์ž‘์„ ๊ฒ€์ฆ

    • Text, State, Child count ๋“ฑ

๋ณต์žกํ•œ ์ปดํฌ๋„ŒํŠธ๋ผ๋ฉด

  • ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ณต์žกํ•˜๋‹ค๋ฉด ์ธํ„ฐ๋ ‰์…˜ ๋‹จ์œ„ ๊ฒ€์ฆ์—์„œ ์ปดํฌ๋„ŒํŠธ์˜ ๋ชฉ์ ๊ณผ ๋™์ž‘์— ๋งž์ถ”์–ด ํ…Œ์ŠคํŠธ๋ฅผ ์ปดํฌ๋„ŒํŠธ๋ณ„ ๋™์ž‘๋ณ„๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์ง„ํ–‰

  • Ex) List - Item ๊ด€๊ณ„ (Container - Child)

    • List ์ปดํฌ๋„ŒํŠธ โ†’ list ์กฐ๊ฑด์— ๋”ฐ๋ผ list count๊ฐ€ ์ž˜๋˜๋Š”์ง€ ํŒŒ์•…

    • Item ์ปดํฌ๋„ŒํŠธ โ†’ child component๋“ค์˜ property, value ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ฌ๋ฐ”๋ฅด๊ฒŒ render ๋˜๋Š”์ง€

๋ Œ๋”๋ง ๊ฒ€์ฆ

Component๋ฅผ Renderํ•˜๊ณ  document.body์— ๋ถ™์ธ๋‹ค.

๋ฐ˜ํ™˜๊ฐ’

  • query objects โ†’ render ๋œ DOM์—์„œ ํŠน์ • element๋ฅผ ์กฐํšŒํ•  ๋•Œ ์‚ฌ์šฉ === screen ์‚ฌ์šฉ

  • container โ†’ React ์ปดํฌ๋„ŒํŠธ๊ฐ€ render ๋œ DOM node, div

  • baseElement โ†’ container๊ฐ€ ๋“ค์–ด์žˆ๋Š” DOM node, default - body

  • rerender โ†’ props๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ component update ์ฒ˜๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉ

  • unmount โ†’ ๋ฉ”๋ชจ๋ฆฌ ์œ ์ˆ˜๋ฅผ ์œ ๋ฐœํ•˜๋Š” event handler๋ฅผ ๋‚จ๊ฒจ๋‘๋Š” ํ…Œ์ŠคํŠธ ์‹œ๋„ ์‹œ ์œ ์šฉ, ๋˜๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ unmount ๋˜๋Š” ์‹œ์ ์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ

RenderHook

ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Hook์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ํ•จ์ˆ˜

import { renderHook } from '@testing-library/react';
  • React v18 ๋ถ€ํ„ฐ RTL/react์— ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จ๋˜์–ด์ง

  • renderHook ํ•จ์ˆ˜ ์•ˆ์— ์ฝœ๋ฐฑ์œผ๋กœ hook์„ ๋„ฃ๊ณ  ํ…Œ์ŠคํŠธ

  • return object์˜ reulst ์†์„ฑ์„ ํ†ตํ•ด ํ›…์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • result.current โ†’ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฆฌํ„ดํ•œ ๊ฐ’

  • current โ†’ ์ปค์Šคํ…€ ํ›…์€ ์—ฌ๋Ÿฌ๋ฒˆ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰ ์‹คํ–‰๊ฐ’์ธ current๋ฅผ ์ฐธ์กฐ

// ์ž˜๋ชป๋œ ํ…Œ์ŠคํŠธ (ํ›…์€ ํ•จ์ˆ˜ํ˜• ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.)
test('Check initial value of isOpen is false', () => {
  const result = useOpen();
  expect(result.open).toBe(false);
})

// 'renderHook'์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ›… ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•
test('Check initial value of isOpen is false', () => {
  let useOpenRes = {} as ReturnType<typeof useOpen>;
  const Wrapper = () => {
    useOpenRes = useOpen();
    return <></>;
  }
  
  render(<Wrapper />);
  
  expect(useOpenRes.open).toBe(false);
})

// Better
test('Check initial value of isOpen is false', () => {
  const { result } = renderHook(() => useOpen());
  expect(result.current.open).toBe(false);
})

test('Check isOpen updated when setOpen is called', () => {
  const { result } = renderHook(() => useOpen());
  expect(result.current.open).toBe(false);
  
  act(() => result.current.setOpen(true));
  
  expect(result.current.open).toBe(true);
})

Context API ๊ฒ€์ฆ

custom hook์ด context API ๋˜๋Š” redux์˜ dispatch๊ฐ™์€ context๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

renderHook์˜ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ contextProvider Wrapper๋ฅผ ์ œ๊ณต

test('Should use custom step when incrementing', () => {
  const wrapper: React.FC<PropsWithChildren> = ({ children }) => {
    <CustomStepProvider step={2}>{children}</CustomStepProvider>
  }
  const { result } = renderHook(() => useCounter(), { wrapper });
  
  act(() => { result.current.increment });
  
  expect(result.current.count).toBe(2);
})

/* const CustomStepProvider = ({ step, children }) => (
*  <CustomStepContext.Provider value={step}>{children}</CustomStepContext>
* )
*/

Asyc ๊ฒ€์ฆ

custom hook์ด async ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ๋•Œ

describe('useCounter with async test', () => {
  beforeEach(() => {
    jest.useFakeTimers();
  });
  afterEach(() => {
    jest.runOnlyPendingTimers();
    jest.useRealTimers();
  });
  
  test('should increment counter after delay', async () => {
    const { result } = renderHook(() => useCounter());
    
    result.current.incrementAsync();
    
    await act(() => jest.runAllTimers());
    
    expect(result.current.count).toBe(1)
  })
})

Last updated