React 组件 安装 react 依赖 yarn add react react-dom 和声明 yarn add -D @types/react安装 react 测试库 yarn add -D @testing-library /react @testing-library/jest-dom 添加 typescript 配置文件 tsconfig.json { "compilerOptions": { /react'; import '@testing-library/jest-dom/extend-expect'; import Title from '.. /react'; import '@testing-library/jest-dom/extend-expect'; import Count from '.. /react'; import '@testing-library/jest-dom/extend-expect'; import Button from '..
使用 @testing-library/user-event 库模拟用户事件。 对呈现的输出进行断言。 以下测试中,我们依据上述的步骤来验证 Counter 组件的功能: import { render, screen } from '@testing-library/react' import { Counter /Counter' import user from '@testing-library/user-event' describe('Counter', () => { test('renders }; }; 接着,让我们来探索一下如何使用 React Testing Library 对它进行测试: // useCounter.test.tsx import { render } from "@testing-library 因此,我们对测试代码进行如下更改: // useCounter.test.tsx import { renderHook, act } from '@testing-library/react' import
安装和配置首先,确保你已经安装了react, react-dom, jest, @testing-library/react, 和 @testing-library/jest-dom。 在你的package.json中添加以下依赖:npm install --save-dev jest @testing-library/react @testing-library/jest-dom# 或yarn add --dev jest @testing-library/react @testing-library/jest-dom在jest.config.js中配置Jest,例如:module.exports = { setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'], testEnvironment: 'jsdom',};基本测试结构创建一个测试文件 下面是一个简单的组件测试示例:import React from 'react';import { render, fireEvent, screen } from '@testing-library/
@testing-library/jest-dom 是一个用于增强 Jest 测试框架的库,它提供了一组用于 DOM 断言的定制化匹配器和工具函数。 当需要基于DOM元素进行匹配测试时,推荐引入@testing-library/jest-dom。 @testing-library/user-event 是一个用于模拟用户事件的 JavaScript 库。 /react'; import React from 'react'; import '@testing-library/jest-dom'; import userEvent from '@testing-library Form } from 'antd'; import '@testing-library/jest-dom'; import { renderHook } from '@testing-library
一般也用 @testing-library 来搭配 vitest,提供 DOM 等核心测试能力。 LoginForm.stories.js|jsx import React from 'react'; import { within, userEvent } from '@storybook/testing-library FilledForm.play = async ({ canvasElement }) => { const canvas = within(canvasElement); // 复用单测中的 testing-library 需要做的也非常简单,直接在单测中 import 后 play 就是了: // foo.spec.jsx import { render } from '@testing-library/react';
比如: import {render, screen} from '@testing-library/react' import userEvent from '@testing-library/user-event 这就是为什么会有 @testing-library/react-hooks,如果我们用了它,会变成这样: import {renderHook, act} from '@testing-library/ 实际上,@testing-library/react-hooks 底层也是做了一些和我们上面 setup 类似的事。 当然,肯定会有更复杂的 Hooks,使用 @testing-library/react-hooks 则更有用。 好了,这篇外文就给大家带到这里了。 这篇文章也给我们带来了两种测试 Hooks 的思路:使用 Test Componet 以及 @testing-library/react-hooks。
,这次我将继续使用 @testing-library/react 来测试我们的 React 应用,并简要简要说明如何测试异步组件。 import React from "react"; import { render } from "@testing-library/react"; import Login from ". /react"; import userEvent from "@testing-library/user-event"; import Login from ". /登录/ })); expect(submittedData).toEqual({ username, password }); }); 我们可以选用 @testing-library 现在我们已经有了 mock,让我们来渲染组件,并且界面显示 loading: import React from "react"; import { render, screen } from "@testing-library
/react' import userEvent from '@testing-library/user-event' import {client} from '~/utils/api-client' /react' import userEvent from '@testing-library/user-event' beforeAll(() => jest.spyOn(window, 'fetch /react' import userEvent from '@testing-library/user-event' test('clicking "confirm" submits payment /react' import userEvent from '@testing-library/user-event' test('clicking "confirm" submits payment /react' import userEvent from '@testing-library/user-event' // 啥也不需要改 test('clicking "confirm" submits
App 组件是这样的: 它的单测是这么写的: 通过 @testing-library/react 的 render 函数把组件渲染出来。 第一种方法的 screen 是 @testing-library/react 提供的 api,是从全局查找 dom,可以直接根据文本查(getByText),根据标签名和属性查(getByRole) 等 import { render, fireEvent } from '@testing-library/react'; import Toggle from '. 这个 createEvent 也是 @testing-library/react 提供的 api,用来创建事件的。 绝大多数情况下,直接 fireEvent.xxx 就好了。 主要是用 @testing-library/react 这个库,它有一些 api: render:渲染组件,返回 container 容器 dom 和其他的查询 api fireEvent:触发某个元素的某个事件
,这次我将继续使用 @testing-library/react 来测试我们的 React 应用,并简要简要说明如何测试路由系统。 测试方法 我们知道 @testing-library/react 是运行在 node 环境中的,但浏览器中并没有 HashRouter 或者 BrowserRouter ,所以我们需要一个用到 MemoryRouter </Routes> </HashRouter> ) } export default App 此时我们可以添加单元测试 import { render, screen } from '@testing-library /react' import userEvent from '@testing-library/user-event' import { createMemoryHistory } from 'history
,这次我将继续使用 @testing-library/react 来测试我们的 React 应用,并简要简要说明如何测试路由系统。 测试方法 我们知道 @testing-library/react 是运行在 node 环境中的,但浏览器中并没有 HashRouter 或者 BrowserRouter ,所以我们需要一个用到 MemoryRouter Routes> </HashRouter> ); } export default App; 此时我们可以添加单元测试 import { render, screen } from "@testing-library /react"; import userEvent from "@testing-library/user-event"; import { createMemoryHistory } from "history
) // ✅ import {render, screen} from '@testing-library/react' 现在cleanup 都是自动调用的,所以你已经不再需要再考虑它了。 直接在 render 引入的时候一并引入就可以了: import {render, screen} from '@testing-library/react' 使用 screen 的好处是:在添加/删除 建议:用 @testing-library/jest-dom 这个库 将不必要的操作放在 act 里 重要程度:中 // ❌ act(() => { render(<Example />) }) 我们现在还在进行 @testing-library/user-event 这个库的开发,来保证它能像它承诺的那样:能够触发用户在执行特定操作时会触发的所有相同事件。 不过,现在它还没完全做到这一点,这也是为什么它还没有合入 @testing-library/dom (可能在未来的某个时候会合入)。
/react"; import userEvent from "@testing-library/user-event"; import { AppProvider } from "@/providers not.toBeInTheDocument(); }); }, { timeout: 5000, } ); }; export * from "@testing-library // src/testing/setup-tests.ts import "@testing-library/jest-dom/extend-expect"; import { queryClient # 安装及配置 Cypress 安装 Cypress pnpm add -D cypress @testing-library/cypress 配置 Cypress cypress.config.ts command -- // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) import "@testing-library
: { "name": "first-react-app", "version": "0.1.0", "private": true, "dependencies": { "@testing-library /jest-dom": "^5.14.1", "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^
以下是具体方法和工具使用指南:核心工具与原理@testing-library/react-hooks:专门为测试 Hooks 设计的库,提供了模拟组件环境的能力核心原理:通过创建一个临时的 React 安装依赖npm install --save-dev @testing-library/react-hooks react-test-renderer# 或yarn add --dev @testing-library
react源码部分的实现 环境 React 17.0.2 目录结构 创建项目 首先npx create-react-app xxx 降为17 "dependencies": { "@testing-library /jest-dom": "^5.11.4", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^
/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2 /user-event@7.2.1" has unmet peer dependency "@testing-library/dom@>=5". [4/4] Building fresh packages /jest-dom@4.2.4 ├─ @testing-library/react@9.4.0 ├─ @testing-library/user-event@7.2.1 ├─ @types/jest@24.9.0 /dom@6.11.0 ├─ @testing-library/jest-dom@4.2.4 ├─ @testing-library/react@9.4.0 ├─ @testing-library/user-event /user-event@7.2.1" has unmet peer dependency "@testing-library/dom@>=5". success Uninstalled packages
package.json 示例 { "dependencies": { "@testing-library/jest-dom": "^5.11.4", "@testing-library /react": "^11.1.0", "@testing-library/user-event": "^12.1.10", "react": "^17.0.2",
: { "name": "first-react-app", "version": "0.1.0", "private": true, "dependencies": { "@testing-library /jest-dom": "^5.14.1", "@testing-library/react": "^11.2.7", "@testing-library/user-event": "^
import React from "react"; import { render, fireEvent } from "@testing-library/react"; import Counter App.test.js import React from 'react' import {render, cleanup} from '@testing-library/react' import App { render, cleanup, fireEvent } from '@testing-library/react'; import TestEvents from '.. TestRouter.test.js import React from 'react' import "@testing-library/jest-dom/extend-expect"; import { Router } from 'react-router-dom' import { render, fireEvent } from '@testing-library/react' import