OverReact 컴포넌트를 위한 Dart 단위 테스트 라이브러리로, JavaScript의 react-testing-library와 유사한 익숙한 API를 제공합니다. 이 라이브러리는 구현 세부 사항보다 컴포넌트의 동작에 초점을 맞춰 직관적이고 사용자 중심의 테스트를 작성할 수 있게 해줍니다. 웹 플랫폼을 지원하며 Flutter의 테스트 생태계와 원활하게 통합됩니다. 사용자가 실제로 상호작용하는 것처럼 렌더링된 UI 요소를 테스트하도록 장려함으로써 견고한 테스트 관행을 촉진합니다.
사용 사례
OverReact 컴포넌트 렌더링 테스트
웹 앱에서 사용자 상호작용 검증
접근성 있고 유지보수 가능한 UI 테스트 작성
실제 사용자 시나리오 시뮬레이션
컴포넌트의 동작이 기대에 부합함을 보장합니다
주요 기능
JS react-testing-library 에서 익숙한 API
사용자 중심 테스트에 집중하기
웹 플랫폼만 지원
렌더링된 출력 테스트를 장려합니다
DOM 유사 구조를 쿼리하는 데 사용할 수 있는 유틸리티 제공
적합한 대상
웹 프로젝트에서 OverReact를 사용하는 개발자
테스트에서 접근성과 사용성을 우선시하는 팀
간단하고 가독성 있는 테스트 문법을 원하는 테스터
일관된 UI 테스트 패턴이 필요한 프로젝트
OverReact 컴포넌트를 사용한 Flutter 웹 애플리케이션
고려 사항
웹 플랫폼에만 제한됨
컴포넌트 테스트를 위해 OverReact가 필요합니다
OverReact가 아닌 Flutter 컴포넌트에는 적합하지 않습니다
새 사용자에게는 학습 곡선이 필요할 수 있습니다
Workiva의 유지보수 및 업데이트에 의존
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Testing Library for Dart
Dart bindings for the JS dom-testing-library and react-testing-library packages, which provide simple and complete DOM/React testing utilities that encourage good testing practices.
To paint the full picture of how / why to use this library, please read the JS Testing Library Introduction, Guiding Principles and FAQs.
How Does It Work?
Rather than dealing with OverReactUiComponent instances, or React VDom ReactElements, your tests will work with actual DOM nodes. The utilities this library provides facilitate querying the DOM in the same way the user would. Finding form elements by their label text (just like a user would), finding links and buttons from their text (like a user would). It also exposes a recommended way to find elements by an HTML data-test-id attribute value as an "escape hatch" for elements where the text content and label do not make sense or is not practical.
This library encourages your Dart web applications to be more accessible and allows you to get your tests closer to using your components the way a user will, which allows your tests to give you more confidence that your application will work when a real user uses it.
dependencies:# This is not required to use this library, but it is assumed# that you are testing OverReact UI components, so it is # shown here for completeness.over_react:^4.0.0dev_dependencies:build_runner:">=1.7.1 <3.0.0"build_test:">=0.10.9 <3.0.0"build_web_compilers:">=2.12.0 <4.0.0"react_testing_library:^1.1.13test:^1.14.4# This is not technically required, # but makes the HTML portion of your test bootstrapping much easier!test_html_builder:^2.0.0
2. Configure build.yaml to generate test outputs
build.yaml
targets:$default:builders:build_web_compilers|entrypoint:# These are globs for the entrypoints you want to compile.generate_for:-test/**.browser_test.dart
Use the template .html file above and follow the test_html_builder library instructions for wiring it up!
Adding / committing your own HTML file(s)
If for some reason you do not wish to use the test_html_builder library to generate the necessary .html file(s), you must create one for each analogous *_test.dart file in which you are using react_testing_library as shown below. Note that you will have to have one .html file for each .dart file containing your unit tests.
test/unit/some_unit_test.html
<!DOCTYPE html><htmllang="en"><head><title>Some Unit Test</title><scriptsrc="packages/react/js/react.dev.js"></script><scriptsrc="packages/react_testing_library/js/react-testing-library.js"></script><linkrel="x-dart-test"href="some_unit_test.dart"><scriptsrc="packages/test/dart.js"></script></head></html>
5. Write test(s) for your component(s)!
Using the render function, you can now generate DOM using React components and query within that DOM to get the element(s) you want to test!
Then, you can use the Matchers from the matchers.dart entrypoint to perform assertions on that DOM, and the events and user events entrypoints or whatever to interact with the DOM.
import'dart:html';
import'package:test/test.dart';
import'package:react_testing_library/react_testing_library.dart'as rtl;
import'package:react_testing_library/matchers.dart';
import'package:your_package/src/components/greeting.dart';
main() {
group('Greeting', () {
rtl.RenderResult view;
tearDown(() {
view = null;
});
group('renders the expected elements:', () {
group('a <header>', () {
HtmlElement headerElement;
setUp(() {
view = rtl.render((Greeting()
..heading = 'Hello There!'
..subHeading = 'Welcome to the unit testing party.'
..getStartedRoute = '/start/partying'
)());
headerElement = view.getByRole('group');
});
test('with a child <h1> with a text value equal to props.heading', () {
final h1Element = rtl.within(headerElement).getByRole('heading', level: 1);
expect(h1Element, hasTextContent('Hello There!'));
});
test('with a child <h2> with a text value equal to props.subHeading', () {
final h2Element = rtl.within(headerElement).getByRole('heading', level: 2);
expect(h2Element, hasTextContent('Welcome to the unit testing party.'));
});
test('with a child <a> with an href equal to props.getStartedRoute', () {
final anchorElement = rtl.within(headerElement).getByRole('link');
expect(anchorElement, hasAttribute('href', '/start/partying'));
});
});
});
});
}
The react_testing_library Dart package strives to maintain API parity with the analogous JS testing-library packages within reason. However, there are certain differences that are either unavoidable, or intentional for the purposes of building Dart APIs that are easy to use and reason about.
Read more about Dart / JS API Parity for the following API categories: