Reactjs 使用Jest进行本机反应-无法读取属性<;propName>;未定义的

Reactjs 使用Jest进行本机反应-无法读取属性<;propName>;未定义的,reactjs,react-native,jestjs,Reactjs,React Native,Jestjs,我正在使用Jest和React测试呈现程序测试React本机组件,在该组件中,我向装载时的React导航对象添加了一些参数,如下所示: componentDidMount() { const { setParams } = this.props.navigation; setParams({ subTitle: this.props.user.outlet.name.toUpperCase() }); } 这在UI中工作正常,但在测试中我得到错误TypeError:c

我正在使用Jest和React测试呈现程序测试React本机组件,在该组件中,我向装载时的React导航对象添加了一些参数,如下所示:

  componentDidMount() {
    const { setParams } = this.props.navigation;

    setParams({ subTitle: this.props.user.outlet.name.toUpperCase() });
  }
这在UI中工作正常,但在测试中我得到错误
TypeError:cannotreadproperty'outlet'of undefined

作为道具传递
导航
效果很好,但我认为我没有正确地传递
用户
。我做错了什么

这是我的测试(任何我认为不相关的测试):

const props={
导航:{
导航:jest.fn()
},
用户:{
//用户信息在此
}
};
const tree=renderer.create(
);
等待等待(0);
expect(tree.toJSON()).toMatchSnapshot();

更新:

也许不是一个完美的解决方案,但考虑到我无法理解为什么这会失败,首先我通过检查组件中是否存在
user
,使测试通过:

user &&
  user.outlet &&
  navigation.setParams({ subTitle: user.outlet.name.toUpperCase() });

希望有人能解释一下为什么这在一开始就不起作用…

Hmm…我得到
TypeError:setParams不是一个函数
,因为
导航
mock不包括
setParams
user &&
  user.outlet &&
  navigation.setParams({ subTitle: user.outlet.name.toUpperCase() });