【发布时间】:2021-10-08 04:22:47
【问题描述】:
我正在学习使用 Jest 和 Enzyme 在 React 中进行测试,但我不得不不断地重复自己并以冗长的行结束。换句话说,有没有办法改变这个:
describe('<CartItem />', () => {
it('is a reusable component that renders cart items', () => {
expect(shallow(<CartItem drilledProps={{ ...mockProps }} />)).toMatchSnapshot();
});
it('renders once', () => {
expect(shallow(<CartItem drilledProps={{ ...mockProps }} />).length).toEqual(1);
});
})
喜欢这个吗?
describe('<CartItem />', () => {
const CartItem = <CartItem drilledProps={{ ...mockProps }} />;
it('is a reusable component that renders cart items', () => {
expect(shallow(CartItem).toMatchSnapshot());
});
it('renders once', () => {
expect(shallow(CartItem).length).toEqual(1);
});
})
【问题讨论】: