使用 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/