我正在尝试在我的React应用程序中进行快照测试。我已经使用react-testing-library进行单元测试。然而,在进行快照测试时,我在网络上看到了不同的方法,可以使用react-test-renderer或react-testing library。以下是三个示例,请问它们之间有什么区别,哪种更受欢迎?
1. 使用react-test-renderer
test('renders correctly', () => {
const tree = renderer
.create(<Link page="http://www.facebook.com">Facebook</Link>)
.toJSON();
expect(tree).toMatchSnapshot();
});
2. 使用 react-testing-library 和 asFragment()
test('renders correctly', () => {
const { asFragment } = render(<NotFound />);
expect(asFragment()).toMatchSnapshot();
});
3. 使用 react-testing-library 和 container
test('renders the component', () => {
const container = render(<Component />)
expect(container.firstChild).toMatchSnapshot()
})