javascript - 无法在 react 原生 Jest 中记录 enzyme 浅包装器的值

标签 javascript react-native jestjs enzyme

使用 React Native、jest 和 enzyme ,我什至无法检查浅层渲染组件的值,更不用说对其运行测试断言了。

Enzyme 和 jest 对于其他测试文件运行良好。

我的控制台中没有发生任何错误的日志输出。

import React from 'react';
import { shallow } from 'enzyme';
import { SomeComponent } from '../SomeComponent';

describe('SomeComponent', () => {
  it('renders', () => {
    const props = { name: 'hey' }
    const shallowWrap = shallow(<SomeComponent {...props} />);
    console.log(shallowWrap) // this wont even log
    expect(shallowWrap).toMatchSnapshot();
  });
});

最佳答案

try {
  const shallowWrap = shallow(<SomeComponent {...props} />);
} catch (e) {
  console.log(e)
}

围绕组件的浅化包裹一个 try/catch 并记录错误,显示我在 SomeComponent 的渲染函数中抛出了一个错误。

修复了该错误的原因,现在工作完美。

关于javascript - 无法在 react 原生 Jest 中记录 enzyme 浅包装器的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50063609/

相关文章:

javascript - 如何在 GatsbyJS 节点中添加自定义 GraphQL 参数?

javascript - ApolloClient超时最佳选择

react-native - ImageBackground 不接受 ResizeMode

reactjs - React-Native Animate 查看进出

reactjs - 在React单元测试中将void方法处理为对象的属性

javascript - 在 Edge 浏览器中动态注入(inject) JavaScript 模块

javascript - 使用 React Native 创建平面图

javascript - 在这种情况下,在 Jest 中测试 axios 函数的正确方法是什么?

javascript - '引用错误 : jest is not defined' when running unit test

javascript - 每n秒生成一个随机数js