reactjs - 使用react-testing-library渲染方法不起作用

标签 reactjs jestjs react-testing-library

我尝试测试react-testing-library文档中提供的基本示例。 我完全复制并粘贴了此代码 here

无论如何,它都会失败。

这是失败的测试结果:

shows the children when the checkbox is checked

    TypeError: Cannot read property 'current' of undefined

      34 | test('shows the children when the checkbox is checked', () => {
      35 |   const testMessage = 'Test Message'
    > 36 |   const {queryByText, getByLabelText, getByText} = render(
         |                                                    ^
      37 |     <HiddenMessage>{testMessage}</HiddenMessage>,
      38 |   )
      39 | 

      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:983:55)
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1418:12)
      at render (node_modules/@testing-library/react/dist/pure.js:82:26)
      at Object.<anonymous> (src/components/Authentication/SignUp/index.test.js:36:52)

  ● shows the children when the checkbox is checked

    TypeError: Cannot read property 'current' of undefined

      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:983:55)
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1418:12)
      at node_modules/@testing-library/react/dist/act-compat.js:57:20
      at asyncAct (node_modules/@testing-library/react/dist/act-compat.js:38:14)
      at Object.<anonymous> (node_modules/@testing-library/react/dist/index.js:28:35)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        2.318s, estimated 3s
Ran all test suites related to changed files.

有什么想法吗?

最佳答案

根据我的研究,我也遇到了这个问题,看起来正在调用 currentundefinedhttps://github.com/facebook/react/blob/master/packages/react-dom/src/test-utils/ReactTestUtilsAct.js#L79 内的 IsSomeRendererActing

它看起来像是在 React 包之间共享的全局变量。我能给出的最好建议是检查 React dom React 和 React Test utils 以及任何其他 React 包是否都处于同一版本

关于reactjs - 使用react-testing-library渲染方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57535536/

相关文章:

reactjs - 错误 TS2604 : JSX element type '...' does not have any construct or call signatures

javascript - 倒计时在某些情况下不起作用

node.js - 在 React 组件的 setState 中使用 Math.random 时组件渲染不正确

node.js - Google Cloud Datastore 开 Jest nodejs node_modules

reactjs - @testing-library/react TDD,react-bootstrap : Is this kind of test useful or just time loose

javascript - 为什么状态更改不会调用 React act() 错误?

javascript - React-Testing-Library:使用 typescript 无法将组件传递给渲染函数

reactjs - 如何找出哪个 React 组件抛出空 props 错误

javascript - Enzyme/Jest Prop 测试失败

Angular 8 和 jest - 找不到文件 : jest-preset-angular/InlineHtmlStripStylesTransformer. js