gpt4 book ai didi

testing - console.log(Wrapper.debug()) 不显示 react 组件

转载 作者:行者123 更新时间:2023-11-28 20:16:43 32 4
gpt4 key购买 nike

我正在使用 enzyme with jest 并尝试打印出 wrapper.debug 但我没有得到如 jest 文档中所示的输出。这里有什么问题?我的测试文件:

import React  from 'react';
import Enzyme, {shallow} from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';
import App from './../../components/App/App';



Enzyme.configure({ adapter : new EnzymeAdapter() });

it('should render without crashing', () => {
const wrapper = shallow(<App />)


console.log(wrapper.debug());

});

我的控制台输出是这样的:

 PASS  src/test/integration/App.test.js
● Console

console.log src/test/integration/App.test.js:14
<ContextConsumer>
[function bound renderWrappedComponent]
</ContextConsumer>


Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.8s
Ran all test suites related to changed files.

最佳答案

浅层渲染对于将组件作为一个单元进行测试很有用,因此它不会潜入并在 <ContextConsumer> 中寻找您的组件。 .

一些选项:

  • 您可以直接浅化 <ContextConsumer> 中的组件.
  • 您可以使用 mount 更改 shallow 以渲染完整的 DOM 渲染(这里可能不是您的意图)。
  • 使用.dive()浅渲染当前包装器的一个非 DOM 子级。 Doc

关于testing - console.log(Wrapper.debug()) 不显示 react 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54250492/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com