💻 Programming

Cypress

A frontend E2E testing framework

📖 Detailed Explanation

Cypress is a next-generation front-end testing framework that was first developed in 2015 and released to the public in 2017. Unlike existing Selenium-based tools, it runs directly inside the browser and has native access, which makes it much easier and more stable to perform network request mocking, DOM manipulation, time control, and more.

Cypress's key advantage is 'Time Travel Debugging.' It automatically saves a DOM snapshot at each step during test execution, so when debugging a failed test, you can visually see what went wrong and at which point. In addition, the Auto-waiting feature automatically waits until an element appears, which greatly improves test stability.

In terms of architecture, Cypress consists of two parts: a Node.js process and the inside of the browser. Because the test code runs in the browser in the same event loop as the application, asynchronous operations can be written as if they were synchronous, and timing issues are significantly reduced.

In practice, Cypress supports not only E2E testing but also component testing, API testing, and even visual regression testing. It integrates easily with major CI/CD tools such as GitHub Actions, Jenkins, and CircleCI, and through the Cypress Dashboard it also provides enterprise-grade features such as parallel execution, test analytics, and failure recordings.

💻 Code Example

// cypress/e2e/login.cy.js - 로그인 E2E 테스트
describe('로그인 기능', () => {
    beforeEach(() => {
        // 각 테스트 전에 로그인 페이지 방문
        cy.visit('/login');
    });

    it('올바른 자격증명으로 로그인 성공', () => {
        // 입력 필드 조작
        cy.get('[data-cy="email"]').type('user@example.com');
        cy.get('[data-cy="password"]').type('secure123');
        cy.get('[data-cy="submit"]').click();

        // 로그인 성공 확인
        cy.url().should('include', '/dashboard');
        cy.get('[data-cy="welcome-message"]')
            .should('contain', '환영합니다');
    });

    it('잘못된 비밀번호로 에러 메시지 표시', () => {
        cy.get('[data-cy="email"]').type('user@example.com');
        cy.get('[data-cy="password"]').type('wrong');
        cy.get('[data-cy="submit"]').click();

        // 에러 메시지 확인
        cy.get('[data-cy="error"]')
            .should('be.visible')
            .and('contain', '비밀번호가 일치하지 않습니다');
    });

    it('API 응답 모킹으로 네트워크 테스트', () => {
        // 네트워크 요청 가로채기
        cy.intercept('POST', '/api/login', {
            statusCode: 200,
            body: { token: 'fake-jwt-token', user: { name: '테스트' } }
        }).as('loginRequest');

        cy.get('[data-cy="email"]').type('test@test.com');
        cy.get('[data-cy="password"]').type('test123');
        cy.get('[data-cy="submit"]').click();

        // API 호출 대기 및 검증
        cy.wait('@loginRequest').its('request.body')
            .should('deep.equal', {
                email: 'test@test.com',
                password: 'test123'
            });
    });
});

🗣️ Say It Like This in Practice

💬 In a Meeting
"Before this release, it would be good to write E2E tests for the key user flows with Cypress. In particular, the payment flow can be tested reliably by mocking the PG company's API with intercept."
💬 In an Interview
"Unlike Selenium, Cypress runs inside the browser and has native access. It has an auto-waiting feature, so there are fewer flaky tests, and time travel debugging makes it easy to find the cause of failures. However, Selenium offers broader cross-browser support."
💬 In a code review
"Please use cy.get().should() instead of cy.wait(3000). Hard-coded wait times can become unstable in CI environments, and making use of Cypress's auto-waiting feature is the best practice."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Using a fixed time with cy.wait()

Avoid hard-coded waits such as cy.wait(5000). Instead, wait explicitly by using cy.get().should('be.visible'), or cy.intercept().as() together with cy.wait('@alias').

❌
Dependencies between tests

Each test should be independent. Reset the state in beforeEach, and set up the login state quickly with cy.session() or cy.request().

✅
Managing selectors with the data-cy attribute

Use test-only attributes such as data-cy="login-button" instead of classes or IDs. Because they are not affected by CSS changes, test maintenance becomes easier.

🔗 Related Terms

📚 Learn More