小编Sat*_*chi的帖子

如何在react-testing-library中获取具有“name”属性的元素

我正在使用反应测试库和笑话来测试我的反应应用程序。

我想知道如何在react-testing-library中获取具有“name”属性的元素。

例如,在下面的页面中,是否可以通过“userName”获取此元素?

<input type="text" name="userName" />
Run Code Online (Sandbox Code Playgroud)

或者我应该设置data-testId到元素并使用getByTestId方法?

return (
  <div>
    <input type="text" name="userName" />
  </div>
)
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs react-testing-library

34
推荐指数
3
解决办法
6万
查看次数

如何使用 craco 为 Jest 设置别名

我正在构建由 create-react-app 提供的 React 应用程序。

\n

我使用 craco 来简化配置,并为 TypeScript 和 Webpack 设置了 alise。

\n

但是当我运行测试命令时,显示以下错误。

\n
    \n
  • 错误信息
  • \n
\n
 spec/showMessage.test.tsx\n  \xe2\x97\x8f Test suite failed to run\n\n    Cannot find module '@/components/atom/TextButton' from 'src/pages/index.tsx'\n\n    Require stack:\n      src/pages/index.tsx\n      spec/showMessage.test.tsx\n\n      3 | import styled from 'styled-components';\n      4 | \n    > 5 | import { TextButton } from '@/components/atom/TextButton';\n        | ^\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是我的 craco.config.js

\n
    \n
  • craco.config.js
  • \n
\n
const path = require("path");\n\nmodule.exports = {\n  jest: {\n    configure: {\n      roots: ['<rootDir>/src', '<rootDir>/spec'],\n      testMatch: …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs craco

9
推荐指数
1
解决办法
7779
查看次数

标签 统计

jestjs ×2

reactjs ×2

craco ×1

react-testing-library ×1