如何在 Create React App 中使用 Enzyme 和 Jest 从安装的组件中读取 console.log

How to read console.log from a mounted component with Enzyme and Jest in Create React App

我有一个正在使用 Enzyme 安装的组件。在组件的构造函数中定义了一个对象。我需要读取该对象内的值。 我该怎么做呢? 如果我使用的是浏览器,我会在构造函数或其他函数中使用 console.log(this.object.property) 。在 Jest 中,这似乎行不通。 我通过以下方式在我的 test() 函数中尝试了 运行 console.log:

console.log(wrapper.instance().object)

但它只显示了对象的默认道具。
我的首选方式是在组件中的任何位置查看 console.log。有没有办法做到这一点?否则,访问 Enzyme 中呈现组件的属性的最佳方法是什么,以便我可以在我的 test() 函数之一中执行 console.log?

添加 --verbose false 将解决问题。
package.json 中的测试行应该类似于:
"test": "react-scripts test --env=jsdom --verbose=false",

就像Console.log statements output nothing at all in Jest建议的那样。

您可以使用console.log(wrapper.debug());