Here’s how you do it using React Testing Library: import { render, screen } from '@testing-library/react'; import Greeting from './Greeting'; test('renders the greeting', () => { render(<Greeting />); screen.debug(); }); When you run this test, screen.debug() will print the...
建议:用 @testing-library/jest-dom 这个库 将不必要的操作放在act里 重要程度:中 代码语言:javascript 代码运行次数:0 运行 AI代码解释 // ❌act(()=>{render(<Example/>)})constinput=screen.getByRole('textbox',{name:/choose a fruit/i})act(()=>{fireEvent.keyDown(input,{key:'ArrowDown'})...
screen 是在DOM Testing Library v6.11.0 引入的 (就就是说,你可以在 @testing-library/react@>=9 这些版本中使用它)。直接在 render 引入的时候一并引入就可以了: import {render, screen} from '@testing-library/react' 使用screen 的好处是:在添加/删除 DOM Query 时,不需要实时地解构 render 的返回...
screen 是在 DOM Testing Library v6.11.0 引入的 (就就是说,你可以在 @testing-library/react@>=9 这些版本中使用它)。直接在 render 引入的时候一并引入就可以了: 复制 import {render, screen} from '@testing-library/react' 1. 使用screen 的好处是:在添加/删除 DOM Query 时,不需要实时地解构 rende...
当你不清楚 RTL 的 render 函数会渲染出什么时,请保持使用 RTL 的debug函数。当你知道渲染的 HTML 的结构后,你才能够通过 RTL 的screen对象的函数进行定位。定位后的元素可用于用户交互或断言。我们会检查元素是否在 DOM 的断言: importReactfrom'react';import{ render, screen }from'@testing-library/react';...
screen是在 DOM Testing Library v6.11.0 引入的(就就是说,你可以在@testing-library/react@>=9这些版本中使用它)。直接在render引入的时候一并引入就可以了: import {render, screen} from '@testing-library/react' 使用screen的好处是:在添加/删除 DOM Query 时,不需要实时地解构render的 ...
React官方推荐testing-library简介和入门 简介 从React官方网站看测试概览。提到了两个比较重要的工具,一个是Jest、一个是React测试库。 Jest是一个JavaScript测试运行器。它允许你使用jsdom操作DOM。尽管jsdom只是对浏览器工作表现的一个近似模拟,对测试React组件来说它通常也已经够用了。
被渲染的组件,可以通过 debug 函数或者 screen 的 debug 函数在控制台输出组件的 HTML 结构。例如下面的 Dropdown 组件的例子: import { render, screen } from '@testing-library/react'; import Dropdown from '../index'; // 要测试的组件 describe('dropdown test', () => { ...
React Testing Library是一个用于测试React组件的工具库。它提供了一系列工具和API,帮助开发者编写可靠的、易维护的、高效的单元测试。 在React Testing Library中测试单击事件的方法如下: 导入所需的测试工具和React组件: 代码语言:txt 复制 import { render, screen, fireEvent } from '@testing-library/react'; ...
screen screen的方法继承自queries, queries有的能力都有,此外还增加了debug()的能力.(1) 关于更多自定义的东西,例如render、选择器以及与Jest有的配置,查阅 官方文档 吧,写得也是很清楚哦!(2)对于dom的一些断言,可以添加testing-library库提供的jest-dom/extend-expect来更好得对dom进行断言。...