Using Jest and Enzyme for Unit Testing
Installing Testing Tools
First, install Jest and Enzyme in your project by running the following command in the project root directory:
npm install --save-dev jest enzyme enzyme-adapter-react-16Note: enzyme-adapter-react-16 is the adapter for React 16. If you’re using a different React version, install the corresponding adapter.
Configuring Jest
Create or modify a jest.config.js file in the project root to configure Jest options:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
};Here, we use the ts-jest preset for TypeScript projects, set testEnvironment to jsdom to simulate a browser environment, and specify setupFilesAfterEnv to run an initialization script before tests.
Writing Unit Tests
Write unit tests for components using Jest and Enzyme:
// Button.test.js
import React from 'react';
import { shallow } from 'enzyme';
import Button from './Button';
describe('Button component', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<Button />);
});
it('renders without crashing', () => {
expect(wrapper).toHaveLength(1);
});
it('matches the snapshot', () => {
expect(wrapper).toMatchSnapshot();
});
it('calls onClick when clicked', () => {
const mockOnClick = jest.fn();
wrapper.setProps({ onClick: mockOnClick });
wrapper.simulate('click');
expect(mockOnClick).toHaveBeenCalled();
});
});Integration Testing and End-to-End Testing
Integration Testing
Integration tests focus on interactions between multiple components. Use Enzyme’s mount function, which supports the full component lifecycle.
// App.test.js
import React from 'react';
import { mount } from 'enzyme';
import App from './App';
import Button from './Button';
describe('App component', () => {
it('displays a button', () => {
const wrapper = mount(<App />);
expect(wrapper.find(Button)).toHaveLength(1);
});
it('calls button onClick when clicked', () => {
const wrapper = mount(<App />);
const button = wrapper.find(Button);
button.simulate('click');
expect(button.props().onClick).toHaveBeenCalled();
});
});End-to-End Testing
End-to-end (E2E) tests simulate real user interactions, typically using tools like Cypress or Puppeteer. Here’s an example with Cypress:
npm install --save-dev cypressWrite tests in the cypress/integration directory:
// app.spec.js
describe('Application', () => {
beforeEach(() => {
cy.visit('/');
});
it('displays a button', () => {
cy.get('button').should('be.visible');
});
it('responds to clicks', () => {
cy.get('button').click();
cy.get('button').should('have.class', 'clicked');
});
});Production Environment Deployment Configuration
Building the Application
Run npm run build to generate production-ready code. This creates a build directory containing minified JS and CSS files, along with an HTML template.
Deploying to a Server
Upload the build directory contents to your server using FTP, SFTP, or Git. For example, with Git:
git subtree push --prefix=build origin gh-pagesThis pushes the build directory to the gh-pages branch on GitHub, automatically deploying to GitHub Pages.
Configuring a CDN
To accelerate delivery, upload static assets from the build directory to a CDN provider like Cloudflare or AWS CloudFront.
Monitoring and Logging
In production, configure logging and error monitoring with services like Sentry or Loggly to promptly detect and resolve issues.



