Trouble adding react-testing-library to a project - TypeError: Cannot read property 'then' of undefined

Trouble adding react-testing-library to a project - TypeError: Cannot read property 'then' of undefined

将 react-testing-library 添加到项目时遇到问题。

项目目前正在使用这些版本

"react": "16.10.2",
"react-dom": "16.10.2"
"jest": "24.9.0"

我正在为 react-testing-library 添加这些版本

"@testing-library/dom": "^6.10.0",
"@testing-library/jest-dom": "^4.2.2",
"@testing-library/react": "^9.3.1",
"@testing-library/user-event": "^7.1.2"

运行像这样的基本测试

import React from 'react';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

test('react-testing-library hello world', () => {
    const {getByText} = render(
        <div>Hello</div>
    );
    expect(getByText('Hello')).toBeInTheDocument();
});

为 'render()'

获取此错误
TypeError: Cannot read property 'current' of undefined

有什么想法吗?尝试将 react 和 react-dom 更新到最新版本,但仍然得到相同的

看来问题出在我的 jest.setup.js 文档中。我在嘲笑 react-dom with

jest.mock('react-dom');

删除它会使测试 运行 符合预期

我最终升级到这些版本:

  • react: 16.11.0
  • react-dom: 16.11.0
  • react-test-renderer: ^16.11.0
  • jest: 24.9.0
  • node: 10.16.3
  • @testing-library/dom: ^6.10.1
  • @testing-library/jest-dom: ^4.2.3
  • @testing-library/react: ^9.3.2
  • @testing-library/user-event: ^7.1.2

希望这对遇到类似问题的其他人有所帮助!