Jest-DOM 단언(Assertions)

김재훈·2022년 6월 5일
0

Test

목록 보기
2/4

Assertions

  • 단언은 테스트의 통과 여부를 결정합니다

다음 코드는 App.test.js 코드입니다

import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
  render(<App />);
  const linkElement = screen.getByText(/learn react testing/i);
  expect(linkElement).toBeInTheDocument();
});

expect

  • jest global, starts the assertion (jest에서 전역 메서드인 expect 메서드로 시작)

expect argument(인수)

  • subject of the assertion (예측하는 요소, 예측이 들어맞는지 jest에서 확인하기 위해)

matcher(매처)

  • type of assertion (assertion의 유형)
  • this matcher (toBeInTheDocument) comes from Jest-DOM
    (위 코드에서 매처는 Jest DOM에서 온 toBeInTheDocument)

jest-dom

  • comes with create-react-app (create-react-app 설치 시, 함께 설치됩니다)
  • src/setup Tests.js imports it before each test, makes matchers available (setupTests.js 파일을 사용해 각 테스트 전에 jest-dom을 가져오므로, 모든 테스트에서 jest-dom 매처를 사용할 수 있습니다)
  • DOM-based matchers (DOM 기반의 매처)
    • toBeInTheDocument, toBeVisible, toBeChecked -> 가상 DOM에만 적용할 수 있습니다
  • toBe, toHaveLength는 일반적인 매처 -> 모든 node코드에 적용할 수 있습니다
profile
같이 협업하며 성장하고싶은, 좋은 개발자를 지향합니다 :)

0개의 댓글