const { getByText, debug } = render(<MyComponent />); const helloText = getByText('Hello'); debug(helloText); Read More: Top Testing Libraries for React in 2023 How to debug in react testing library using screen.debug() method? Suppose you have a component that just displays a gre...
screen是在 DOM Testing Library v6.11.0 引入的 (就就是说,你可以在@testing-library/react@>=9这些版本中使用它)。直接在render引入的时候一并引入就可以了: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 import{render,screen}from'@testing-library/react' 使用screen的好处是:在添加/删除 DOM Query...
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 函数通过 JSX 去渲染内容,然后,你就能在测试代码中访问你的组件,通过 RTL 的 debug 函数,可以确保看到渲染的内容: importReactfrom'react';import{ render, screen }from'@testing-library/react';importAppfrom'./App';describe('App',() =>{test('renders App component',() =>{render(<Ap...
screen是在 DOM Testing Library v6.11.0 引入的(就就是说,你可以在@testing-library/react@>=9这些版本中使用它)。直接在render引入的时候一并引入就可以了: import {render, screen} from '@testing-library/react' 使用screen的好处是:在添加/删除 DOM Query 时,不需要实时地解构render的 ...
screen.debug(); 输出将为: START Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ac commodo massa END 正如您所看到的,整个文本都已呈现,而使文本可见/不可见的是cssoverflow属性。 也就是说,我认为这里最好的方法就是验证组件是否具有overflow属性: expect(screen.getByText(/^START/)...
React官方推荐testing-library简介和入门 简介 从React官方网站看测试概览。提到了两个比较重要的工具,一个是Jest、一个是React测试库。 Jest是一个JavaScript测试运行器。它允许你使用jsdom操作DOM。尽管jsdom只是对浏览器工作表现的一个近似模拟,对测试React组件来说它通常也已经够用了。
import { render, screen } from '@testing-library/react'; import Dropdown from '../index'; // 要测试的组件 describe('dropdown test', () => { it('render Dropdown', () => { // 渲染 Dropdown 组件 const comp = render(<Dropdown />); comp.debug(); screen.debug(); // 这两种...
建议:尽可能地使用@testing-library/user-event,而不是fireEvent 没有用query*来断言元素不存在 重要程度:高 // ❌expect(screen.queryByRole('alert')).toBeInTheDocument()// ✅expect(screen.getByRole('alert')).toBeInTheDocument()expect(screen.queryByRole('alert')).not.toBeInTheDocument() ...