要比较使用Jest/Enzyme生成的HTML与React JSX,可以使用Jest和Enzyme提供的断言方法来进行比较。以下是一个示例解决方法:
npm install --save-dev jest enzyme enzyme-adapter-react-16
// MyComponent.jsx
import React from 'react';
const MyComponent = () => {
return (
Hello, Jest/Enzyme!
This is a test component.
);
};
export default MyComponent;
// MyComponent.test.js
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
it('renders the correct HTML', () => {
const wrapper = shallow( );
const expectedHTML = (
Hello, Jest/Enzyme!
This is a test component.
);
expect(wrapper.html()).toEqual(shallow(expectedHTML).html());
});
});
在上面的示例中,我们使用shallow
方法来浅渲染React组件,并使用html
方法获取生成的HTML。然后,我们创建了一个预期的HTML片段,并使用toEqual
方法来比较两者是否相等。
这样就可以使用Jest和Enzyme来比较使用Jest/Enzyme生成的HTML与React JSX了。