【问题标题】:Cannot read property of getstate of undefined error in react testing在反应测试中无法读取未定义错误的getstate属性
【发布时间】:2020-07-02 17:28:33
【问题描述】:

我正在尝试为 App 组件编写一个非常简单的测试用例,但在 React.DOM.render 行中出现“无法读取未定义的属性 getState”错误。我是测试新手。

应用程序

const store = StoreConfig();

function App(props) {
  return (
    <Provider store={store}>
      <Home />
    </Provider>
  );
}

App.spec.js

it('renders correctly', () => {
  const div = document.createElement('div');
  ReactDOM.render(
    <Provider store={store}>
      <App />
    </Provider>,
    div
  );
  ReactDOM.unmountComponentAtNode(div);
});

【问题讨论】:

  • 什么是 StoreConfig() ?

标签: reactjs testing redux jestjs


【解决方案1】:

样板 CRA 测试如下:

也许对于您的 linkElement 变量,您可以搜索/匹配您知道应该由您的 App 组件呈现的文本。

import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
  const { getByText } = render(<App />);
  const linkElement = getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
});

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 2022-10-05
    • 1970-01-01
    • 2019-07-17
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    相关资源
    最近更新 更多