console.log(Wrapper.debug()) 没有显示反应组件

sri*_*gde 7 testing jestjs enzyme create-react-app

我正在使用带有玩笑的酶并尝试打印出 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());

});
Run Code Online (Sandbox Code Playgroud)

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

 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.
Run Code Online (Sandbox Code Playgroud)

Gib*_*boK 1

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

一些选项:

  • 您可以直接浅化其中的组件<ContextConsumer>
  • 你可以用 mount 来改变shallow,它会渲染完整的 DOM 渲染(可能不是你的意图)。
  • 使用.dive()深度渲染当前包装器的一个非 DOM 子级。医生

  • 你能写代码吗,因为我是反应酶和整个测试的初学者 (3认同)