Cypress से End-to-End Testing
In this page:
describe("feature", () => {
it("description", () => {
cy.visit("/path");
cy.get("selector").type("text");
cy.contains("button", "label").click();
cy.contains("expected text").should("be.visible");
});
});
एक Test को 'End-to-End' क्या बनाता है
Unit या integration tests के विपरीत जो isolated pieces check करते हैं, एक end-to-end (E2E) test आपके ACTUAL running app को एक real browser में drive करता है, एक complete user journey simulate करते हुए शुरू से आखिर तक — जैसे एक signup page visit करना, form भरना, submit करना, और एक welcome message appear होना confirm करना।
उदाहरण: What Makes a Test 'End-to-End'
// Run in your local React project (npm install required)
// cypress/e2e/login.cy.js
describe('Login flow', () => {
it('logs in successfully', () => {
cy.visit('/login');
cy.get('input[name=email]').type('[email protected]');
cy.get('button').contains('Log In').click();
cy.contains('Welcome back!').should('be.visible');
});
});
Core Cypress Commands
cy.visit(url) एक page पर navigate करता है, cy.get(selector) elements ढूंढता है (Testing Library की queries जैसी spirit में similar, लेकिन CSS-selector-based), और .click() या .type() जैसे commands उनसे interact करते हैं, सब एक full user flow describe करने के लिए chained together।
उदाहरण: Core Cypress Commands
// Run in your local React project (npm install required)
describe('Search feature', () => {
it('shows results after searching', () => {
cy.visit('/search');
cy.get('input[type=search]').type('react{enter}');
cy.get('.result-item').should('have.length.greaterThan', 0);
});
});
Automatic Retrying और Flaky Tests से बचना
Cypress fail होने से पहले ज़्यादातर assertions को कुछ seconds के लिए automatically retry करता है, जो बहुत सारी async timing (जैसे एक API response के render होने का wait) handle करता है बिना manual sleeps या arbitrary waits के।
यह built-in retrying timing-related test flakiness को कम करता है, हालांकि पूरी तरह eliminate नहीं करता।
उदाहरण: Automatic Retrying and Avoiding Flaky Tests
// Run in your local React project (npm install required)
// Avoid this (fixed, unreliable wait):
// cy.wait(2000);
// cy.get('.result').should('exist');
// Prefer this (Cypress retries automatically until it appears or times out):
cy.get('.result', { timeout: 10000 }).should('exist');
- हर small detail के लिए E2E tests लिखना instead of उन्हें critical, full user journeys के लिए reserve करना।
- E2E tests को real, external, unpredictable data पर depend करवाना instead of एक controlled test environment।
- Async content का properly wait न करना, tests को timing के आधार पर intermittently fail करवाते हुए ('flaky tests')।
- Cypress एक popular end-to-end testing tool है जो एक real browser drive करता है complete user flows simulate करने के लिए।
- cy.visit(), cy.get(), और cy.click() एक real page navigate और interact करने के core commands हैं।
- Cypress automatically assertions को एक short समय के लिए retry करता है, timing-related flakiness को कम करते हुए (हालांकि eliminate नहीं करते)।
- E2E tests unit tests से slower हैं, इसलिए उन्हें critical, high-value user flows के लिए reserve किया जाता है।
npm install cypress चाहिए — Cypress के test runner के through एक real browser run करता है, इस preview sandbox में नहीं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: