我尝试测试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.
有什么想法吗?
最佳答案
根据我的研究,我也遇到了这个问题,看起来正在调用 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 包是否都处于同一版本
关于reactjs - 使用react-testing-library渲染方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57535536/