Enzyme Tests
What is Enzyme?
Why use Enzyme?
…… here’s a list from Enzyme’s own docs about assertions & frameworks that works with it!
But in this case, we will focus on the most popular pairing:
Jest + Enzyme
What’s Jest?
Yep, everything is default with Jest in Create React App!
You get a default feature! You get a default feature!
Enzyme Installation
npm install -D enzyme enzyme-adapter-react-16 react-test-renderer
Configuration
// Inside src/setupTests.js�import { configure } from 'enzyme';�import Adapter from 'enzyme-adapter-react-16';��configure({ adapter: new Adapter() });
Example unit test using Jest + Enzyme
// Production Code�const helloWorld = ({ name }) => <div>Hello {name}!</div>
export default helloWorld;
// Test Code�describe('sum()', () => {� it('renders without crashing', () => {
const welcome = shallow(<helloWorld name='Techtonica' />);� expect(welcome.find('div').text()).toEqual('Techtonica');
});
});
Rendering in Enzyme
Common Enzyme API
Below is some of the more common API. Enzyme has excellent documentation, which list out all available APIs: airbnb.io/enzyme/
Questions about Enzyme?
Testing Resources