我尝试测试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.有什么想法吗?
发布于 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包都是同一版本。
发布于 2019-08-17 20:50:50
此错误可能来自HiddenMessage组件内部,而不是测试代码中的任何错误。看起来您正在尝试访问不存在的东西(可能是ref?)上的current。
测试代码看起来是正确的,您应该跟踪您尝试在HiddenMessage中访问current的位置。
https://stackoverflow.com/questions/57535536
复制相似问题