首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用react-testing library的Render方法不起作用

使用react-testing library的Render方法不起作用
EN

Stack Overflow用户
提问于 2019-08-17 18:44:03
回答 2查看 3.8K关注 0票数 1

我尝试测试react-testing-library文档中提供的基本示例。我准确地复制并粘贴了这段代码here

无论发生什么,它都会失败。

下面是失败的测试结果:

代码语言:javascript
复制
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.

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2019-10-05 05:35:54

我也遇到了这一点,从我的研究来看,current调用的undefined似乎是https://github.com/facebook/react/blob/master/packages/react-dom/src/test-utils/ReactTestUtilsAct.js#L79内部的IsSomeRendererActing

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

票数 4
EN

Stack Overflow用户

发布于 2019-08-17 20:50:50

此错误可能来自HiddenMessage组件内部,而不是测试代码中的任何错误。看起来您正在尝试访问不存在的东西(可能是ref?)上的current

测试代码看起来是正确的,您应该跟踪您尝试在HiddenMessage中访问current的位置。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57535536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档