在测试button时 App.test.tsx 报错

来源:5-3 React 测试工具 - react-testing-library

Lucky雷

2022-09-20

● renders learn react link
TestingLibraryElementError: Unable to find an element with the text: gfjdas. This could be because the text is broken up by multiple elements.
In this case, you can provide a function for your text matcher to make your matcher more flexible.

Ignored nodes: comments, script, style

import React from ‘react’;
import { render, screen } from ‘@testing-library/react’;
import App from ‘./App’;

test(‘renders learn react link’, () => {
render();
// const linkElement = screen.getByText(‘gfjdas’);
// expect(linkElement).toBeInTheDocument();
});

//注释掉这俩行代码的时候正常了
报错信息为:
TestingLibraryElementError:找不到文本为/elearn react/i的标签
忽略的节点:注释、脚本、样式

这个可以怎么解决下

用得是最新的react版本

写回答

2回答

张轩

2022-09-22

同学你好

是因为你的测试用例写的有问题啊。

const linkElement = screen.getByText('gfjdas');
// 这个意思是找里面为 gfjdas 的内容,但是你的 App 组件中,并没有上面的文本,所以报错了。
// 修改为
const linkElement = screen.getByText('Link');
// 你是有这个 Link 文本的,这个时候就通过了


0
0

张轩

2022-09-21

同学你好

请将源代码(git)提供给我一下,我在本地帮你看一下,谢谢

0
1
Lucky雷
https://gitee.com/stupid-lei-lei/vue3demo.git 里边文件夹有点多 leiAntDesign
2022-09-21
共1条回复

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2122 学习 · 959 问题

查看课程