【问题标题】:How to fix: TypeError: ReactWrapper::state("<state>") requires that `state` not be `null` or `undefined`如何修复:TypeError: ReactWrapper::state("<state>") 要求 `state` 不是 `null` 或 `undefined`
【发布时间】:2019-10-10 15:35:58
【问题描述】:

我正在使用 Enzyme 测试 Create-React-App 中的状态。我怎样才能通过这个测试?

当我的 App 组件在我的测试中呈现时,它被包裹在

<BrowserRouter>

(尝试挂载它会产生一个

 Invariant failed: You should not use <Route> outside a <Router>

测试中的错误)。

浅包装产量

 TypeError: Cannot read property 'state' of null

就像安装和包装一样

<BrowserRouter>.

我试过this

结果:未回答的问题

this

结果:未回答的问题

this

结果:卸载 react-test-renderer 没有任何影响

this

结果:我检查了组件中的状态并定义了它。 console.log(wrapper.instance().state) 产生相同的错误:'null'

App.js:

class App extends Component {
  //#region Constructor
  constructor(props) {
    super(props);
    this.state = {
    //... other correctly formatted state variables
      specificRankingOptionBtns: false
    }

app.test.js:

import React from 'react'
import { BrowserRouter } from 'react-router-dom'
import App  from '../../App'
import renderer from 'react-test-renderer'
import { shallow, mount } from 'enzyme';

describe('App', () => {
fit('renders App.js state correctly', () => {
  const wrapper = mount(<BrowserRouter><App /></BrowserRouter>);
  //console.log(wrapper.instance().state);
  //const wrapper = shallow(<App />);
  //const wrapper = mount(<App />);
  console.log(wrapper.instance().state);
  //const wrapper = mount(shallow(<BrowserRouter><App /> 
  //</BrowserRouter>).get(0));
  expect(wrapper.state('specificRankingOptionBtns')).toEqual(false);
});
}

期望:测试通过

实际:“TypeError: ReactWrapper::state("specificRankingOptionBtns") 要求state 不是nullundefined

【问题讨论】:

    标签: react-router tdd enzyme assertion react-wrapper


    【解决方案1】:

    我有同样的问题。这对我有用。

    从 'react-router-dom' 导入 { MemoryRouter as Router };

    it('should find MaskedTextInput in LoginPage', () => {
        const mountWithRouter = node => mount(<Router>{node}</Router>);
        const wrapper = mountWithRouter(<LoginPage {...mockedProps} />);
        const maskedInput = wrapper.find('MaskedTextInput');
    
        const componentInstance = wrapper
            .childAt(0)
            .childAt(0) 
            .instance(); // could also be instance
    
        const mountedState = componentInstance.state.passwordInputType;
        expect(mountedState).toEqual('password');
    });
    

    【讨论】:

    • 谢谢。我进行了导入并将测试更改为:` const mountWithRouter = node => mount({node}); const wrapper = mountWithRouter(); const componentInstance = wrapper .childAt(0) .childAt(0) // 也可以是 .find(Foo) .instance();常量 mountState = componentInstance.state.specificRankingOptionBtns;期望(mountedState).toEqual(假); ` 但我得到了TypeError: Cannot read property 'state' of null。遵循反应测试库原则,我现在不确定是否应该测试内部状态 [?]
    • 尝试 console.log 你的实例,然后你可以看到你的状态在哪里。
    • console.log 在包含对“FiberNode”的引用的长输出末尾给出state: null },Dan Abramov 说“到达那里的唯一方法是通过被认为是内部的 child._owner。所以是的,像这样的深度质量检查是个坏主意。除非有人不同意,否则我认为我不应该测试这个实现细节并重新考虑我的测试方法 [?]。感谢您的 cmets ...
    猜你喜欢
    • 2010-09-21
    • 2023-03-23
    • 1970-01-01
    • 2022-07-21
    • 2018-06-19
    • 2019-03-28
    • 2021-03-09
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多