A React application configured with Cypress for end-to-end testing.
Original blog post located here
- Node.js installed
- npm or yarn package manager
npm installStart the React development server:
npm startThe app will open at http://localhost:3000
Option 1: Interactive Mode (Recommended for development)
-
Start your React app in one terminal:
npm start
-
In another terminal, open Cypress:
npm run cypress:open
-
Select "E2E Testing" in the Cypress UI
-
Choose a browser and click on a test file to run it
Option 2: Headless Mode (For CI/CD)
npm run cypress:runNote: Make sure your React app is running at http://localhost:3000 before running tests
cypress-io-react/
├── cypress/
│ ├── e2e/ # E2E test files
│ │ └── app.cy.js # Sample test
│ ├── fixtures/ # Test data
│ └── support/ # Custom commands and setup
│ ├── commands.js # Custom Cypress commands
│ └── e2e.js # E2E test configuration
├── src/ # React source code
├── public/ # Static assets
├── cypress.config.js # Cypress configuration
└── package.json # Dependencies and scripts
- Base URL:
http://localhost:3000 - Test Type: E2E (End-to-End)
- Config File:
cypress.config.js
- React: ^18.3.1
- React Scripts: 5.0.1
- Cypress: ^15.8.2
- webpack-dev-server: ^5.0.0 (required for Cypress compatibility)
Create new test files in the cypress/e2e/ directory with the .cy.js extension:
describe("My Test Suite", () => {
beforeEach(() => {
cy.visit("/");
});
it("should test something", () => {
cy.get("selector").should("be.visible");
});
});npm start- Start the development servernpm run build- Build for productionnpm run cypress:open- Open Cypress interactive modenpm run cypress:run- Run Cypress tests in headless mode
Make sure your React app is running with npm start before launching Cypress.
If port 3000 is already in use, you can specify a different port:
PORT=3001 npm startThen update the baseUrl in cypress.config.js accordingly.
Feel free to open issues or submit pull requests for improvements.
This project is part of a blog code examples repository.