当前位置:首页 >> 中医丰胸 >> 七爪源码:React 测试 较难初学者

七爪源码:React 测试 较难初学者

发布时间:2025-02-13

; import Todo from '../todo'; afterEach(() => { cleanup(); }); test('should render component', () => { const todo = { id: 1, description: 'learn jascript testing', completed: false, }; render(< Todo todo={todo} />); const todoElement = screen.getByTestId('Nr-1'); expect(todoElement).toBeInTheDocument(); expect(todoElement).toHeTextContent('learn jascript testing'); });

这段标识符首先新的增我们的缓冲器和能够的戈。 然后,在每次试验之前之后,应用于以下标识符讫清理所有具体内容:

import { render, screen, cleanup } from '@testing-library/react'; import Todo from '../todo'; import renderer from 'react-test-renderer'; afterEach(() => { cleanup(); });

下一部分专责恰当的试验之前。 我们将核查我们的缓冲器之前确实带有文本具体内容“学习 JaScript 试验之前”的“软件包依法”。

为此,我们能够公开信我们试验之前的“const todo”(软件包依法之一),然后位图我们试验之前的缓冲器“”。 要通过获取试验之前 id,有必要在被测缓冲器之前命名恰当的 html 成份。 让我们去副本“todo.js”。

import React from 'react'; function Todo({ todo }) { return ( < div data-testid={`Nr-${todo?.id}`}> < h1>{todo?.description} ); } export default Todo;

您可以在我们的 div 成份之前碰到一个新的codice_。 你可以找寻一个“data-testid”,它之和 todo 记录的 id。

每次保留软件后,在端口之前我们可以碰到督导试验之前的反向。

对于这讫标识符,反向如下所示:

PASS src/components/紧接tests紧接/todo.tests.js (9.656 s) √ should render component (84 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 26.342 s Ran all test suites related to changed files. Watch Usage: Press w to show more.

如果我们不够改试验之前的文本具体内容,试验之前将尽快逃逸缺失。 让我们这样做到……

在“todo.tests.js”副本之前,“learn jaScript testing”短语将不够改名“read a book”。

import { render, screen, cleanup } from '@testing-library/react'; import Todo from '../todo'; afterEach(() => { cleanup(); }); test('should render component', () => { const todo = { id: 1, description: 'learn jascript testing', completed: false, }; render(< Todo todo={todo} />); const todoElement = screen.getByTestId('Nr-1'); expect(todoElement).toBeInTheDocument(); expect(todoElement).toHeTextContent('read a book'); });

结果如下所示。

FAIL src/components/紧接tests紧接/todo.tests.js (6.535 s) × should render component (58 ms) ● should render component expect(element).toHeTextContent() Expected element to he text content: read a book Received: learn jascript testing 15 | const todoElement = screen.getByTestId('Nr-1'); 16 | expect(todoElement).toBeInTheDocument(); > 17 | expect(todoElement).toHeTextContent('read a book'); | 1] 18 | }); 19 | at Object. (src/components/紧接tests紧接/todo.tests.js:17:23) Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 10.945 s Ran all test suites related to changed files. Watch Usage: Press w to show more.

结论

这是在 React 软件之前应用于试验之前的最有用方法。 我们只展开了一项核查文本具体内容的试验之前。 事实上,试验之前可以核查软件的几乎所有多方面。 如果您学习 jaScript,请预留几天整整来学习如何改写有用的试验之前并在每个项目之前做到到它!

快乐区块!

海南白癜风医院哪家比较专业
重庆白癜风权威医院
苏州看白癜风哪间医院好
珠海看白癜风去什么医院最好
上海妇科检查费用
有机黄芪和普通黄芪有什么区别
夫西地酸乳膏孕妇能用吗
体质差的人怎么改善体质
999消痔软膏好还是马应龙好
急支糖浆的功效和作用
标签:
友情链接: