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코드에 적용할 수 있습니다