最近推送 2026-08-28 01:40(62 天前),维护频率一般
testing-library/react-testing-library
GITHUB中文整理主语言 JavaScript · 19,675 星 · MIT · 创建于 2018-03-19
一个用于测试 React 组件的 JavaScript 工具库,提供查询和交互 DOM 的实用方法,引导开发者编写贴近用户使用方式的测试。
仓库健康度
5 个维度开放 Issue 85 个 · Fork 1,172 个
star 记录还不够天数,增量算不出来
许可证 MIT · 3 个 topics
这一项暂无数据,不代表项目没有发布
「证据不足」表示这项数据暂时没有取到,不能读成「项目有问题」;「部分数据」表示只拿到一部分信号。版本发布要额外调 Releases 接口,下一版补。
Star 趋势
6 天记录横轴 2026-10-23 → 2026-10-28,纵轴按这段时间的最小/最大值拉伸。趋势来自站内每天记录一次的星数,页面打开时不会实时请求 GitHub。
它解决什么问题
据 README 归纳为 React 组件编写可维护的测试时,如果测试依赖组件内部实现细节,组件重构(只改实现不改功能)就会导致测试失败,拖慢团队效率。这个库帮助避免测试实现细节,让测试关注用户可见的行为,从而在重构后仍能保持稳定。
核心能力
5 条- 1提供基于 DOM 节点的查询方法,模拟用户查找元素的方式
- 2鼓励以用户交互方式触发事件和断言结果
- 3兼容 React Hooks 和类组件,测试写法一致
- 4保持轻量简单,API 设计灵活易理解
- 5可配合 jest-dom 使用自定义断言匹配器
项目画像
6 个角度项目定位
一个用于测试 React 组件的 JavaScript 工具库,提供查询和交互 DOM 的实用方法,引导开发者编写贴近用户使用方式的测试。
适合谁
需要为 React 组件或完整应用编写可维护测试的开发者,尤其是使用 Jest 等测试框架、希望测试不因重构而频繁失效的团队。
典型场景
测试 React 组件在用户点击按钮后是否显示隐藏消息;测试登录表单在提交时的加载、成功和错误状态变化;在 React Redux、React Router 或 Context 项目中编写集成测试
部署难度
通过 npm 安装到 devDependencies,并确保已安装 react、react-dom 等 peer 依赖;16 版起还需额外安装 @testing-library/dom。无需额外构建配置,引入后即可编写测试。
风险限制
需作为 devDependencies 安装,并依赖 react、react-dom;从 16 版起还需安装 @testing-library/dom。13 及以上版本要求 React 18,旧版 React 需使用 12 版。
最近维护
最近一次推送是 2026-08-28 01:40。 实现方式:通过渲染 React 组件到 DOM 环境,暴露查询和事件工具,让测试代码像用户一样查找元素、触发交互并断言页面变化,而不是直接操作组件实例或内部状态。
README 原文细节
5 个板块核心功能README 里列出来的能力清单
💡 Feature Requests
Please file an issue to suggest new features. Vote on feature requests by adding a 👍. This helps maintainers prioritize what to work on.
怎么安装从零跑起来的步骤
Installation
This module is distributed via [npm][npm] which is bundled with [node][node] and should be installed as one of your project's devDependencies. Starting from RTL version 16, you'll also need to install @testing-library/dom:
This library has peerDependencies listings for react, react-dom and starting from RTL version 16 also @testing-library/dom.
React Testing Library versions 13+ require React v18. If your project uses an older version of React, be sure to install version 12:
You may also be interested in installing @testing-library/jest-dom so you can use the custom jest matchers.
npm install --save-dev @testing-library/react @testing-library/dom
怎么用最典型的用法与示例
Basic Example
// hidden-message.js
import * as React from 'react'
// NOTE: React Testing Library works well with React Hooks and classes.
// Your tests will be the same regardless of how you write your components.
function HiddenMessage({children}) {
const [showMessage, setShowMessage] = React.useState(false)
return (
<div>
<label htmlFor="toggle">Show Message</label>
<input
id="toggle"
type="checkbox"
onChange={e => setShowMessage(e.target.checked)}
checke
Complex Example
// login.js
import * as React from 'react'
function Login() {
const [state, setState] = React.useReducer((s, a) => ({...s, ...a}), {
resolved: false,
loading: false,
error: null,
})
function handleSubmit(event) {
event.preventDefault()
const {usernameInput, passwordInput} = event.target.elements
setState({loading: true, resolved: false, error: null})
window
.fetch('/api/login', {
method: 'POST',
headers: {'Content-Type': 'application/js
More Examples
You'll find runnable examples of testing with different libraries in the react-testing-library-examples codesandbox. Some included are:
- react-redux
- react-router
- react-context
实现原理它内部是怎么做的
Guiding Principles
We try to only expose methods and utilities that encourage you to write tests that closely resemble how your React components are used.
Utilities are included in this project based on the following guiding principles:
Most importantly, we want React Testing Library to be pretty light-weight, simple, and easy to understand.
- If it relates to rendering components, it deals with DOM nodes rather than
- component instances, nor should it encourage dealing with component
- instances.
- It should be generally useful for testing individual React components or
- full React applications. While this library is focused on react-dom,
- utilities could be included even if they don't directly relate to
- react-dom.
- Utility implementations and APIs should be simple and flexible.
其它说明README 里剩下的内容
Table of Contents
- The problem
- The solution
- Installation
- Suppressing unnecessary warnings on React DOM 16.8
- Basic Example
- Complex Example
- More Examples
- Guiding Principles
- 💡 Feature Requests
- ❓ Questions
- Contributors
概述
Simple and complete React DOM testing utilities that encourage good testing practices.
The problem
You want to write maintainable tests for your React components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your testbase to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.
关于 react-testing-library 的常见问题
7 个问题react-testing-library 是做什么的?
一个用于测试 React 组件的 JavaScript 工具库,提供查询和交互 DOM 的实用方法,引导开发者编写贴近用户使用方式的测试。 为 React 组件编写可维护的测试时,如果测试依赖组件内部实现细节,组件重构(只改实现不改功能)就会导致测试失败,拖慢团队效率。这个库帮助避免测试实现细节,让测试关注用户可见的行为,从而在重构后仍能保持稳定。
react-testing-library 有哪些功能?
核心能力:提供基于 DOM 节点的查询方法,模拟用户查找元素的方式;鼓励以用户交互方式触发事件和断言结果;兼容 React Hooks 和类组件,测试写法一致;保持轻量简单,API 设计灵活易理解;可配合 jest-dom 使用自定义断言匹配器。
react-testing-library 适合谁用?
需要为 React 组件或完整应用编写可维护测试的开发者,尤其是使用 Jest 等测试框架、希望测试不因重构而频繁失效的团队。
react-testing-library 是怎么工作的?
通过渲染 React 组件到 DOM 环境,暴露查询和事件工具,让测试代码像用户一样查找元素、触发交互并断言页面变化,而不是直接操作组件实例或内部状态。
怎么安装 react-testing-library?
按项目 README 的说明:This module is distributed via [npm][npm] which is bundled with [node][node] and should be installed as one of your project's devDependencie;This library has peerDependencies listings for react, react-dom and starting from RTL version 16 also @testing-library/dom.。完整命令见下方「怎么安装」一节或 GitHub 仓库的 README。
用 react-testing-library 有什么门槛或限制?
需作为 devDependencies 安装,并依赖 react、react-dom;从 16 版起还需安装 @testing-library/dom。13 及以上版本要求 React 18,旧版 React 需使用 12 版。
react-testing-library 现在还在维护吗?数据怎么样?
目前 19,675 星,1,172 个 Fork,主语言 JavaScript,创建于 2018-03-19,最近一次推送 2026-08-28 01:40,许可证 MIT。星数、Fork、推送时间都是 GitHub 返回的实时数据,「最近推送」离现在越近说明越活跃;长期不动要留意是否已停止维护。
这些内容来自哪
读法说明项目数据取自 GitHub 官方接口与项目 README;星数、Fork、语言与时间以 GitHub 页面为准,档案更新于 2026-10-23 17:59。
