wam
w__am 개발노트
wam
  • 분류 전체보기 (165)
    • CS 지식 (10)
      • 자료구조 (0)
      • 알고리즘 (0)
      • 컴퓨터 구조 (0)
      • 운영체제 (0)
      • 네트워크 (7)
      • 데이터베이스 (0)
      • 디자인 패턴 (3)
    • Frontend (131)
      • Three.js (64)
      • NPM (1)
      • Nest.js (19)
      • React (10)
      • Apollo (7)
      • TypeScript (2)
      • JavaScript (12)
      • HTML, CSS (1)
      • Jest (3)
      • E2E (5)
      • Cypress (7)
    • Database (12)
      • TypeORM (12)
    • IT 지식 (8)
      • 클라우드 서비스 (3)
      • 네트워크 (1)
      • 데이터 포맷 (2)
      • 기타 (2)
    • IT Book (2)
    • 유용한 사이트 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 🐱 Github

인기 글

태그

  • 함수 표현식
  • 오프-프레미스(off-premise) 방식
  • 스코프
  • reactive variables
  • 초기 환경설정
  • mapped types
  • Interface
  • react 성능 최적화
  • 함수 선언문
  • getelapsedtime()
  • math.sin()
  • 삼각함수
  • threejs 개발 할 때 도움을 줄 수 있는 유틸리티
  • three.js 구성 요소
  • getdelta()
  • 원형적인 움직임
  • axeshelper
  • joi 에러
  • 렌더링 성능 최적화
  • 디자인 패턴
  • math.cos()
  • 함수의 범위
  • 데이터 포맷
  • isabstract
  • e.preventdefault()
  • gridhelper
  • Decorators
  • API
  • type-graphql
  • 함수 리터럴

최근 글

관리자

글쓰기 / 스킨편집 / 관리자페이지
hELLO · Designed By 정상우.
wam

w__am 개발노트

Cypress, React Testing Library에서 제공하는 비동기 쿼리 메서드 체인 안되는 부분 해결 방법
Frontend/Cypress

Cypress, React Testing Library에서 제공하는 비동기 쿼리 메서드 체인 안되는 부분 해결 방법

2024. 11. 25. 17:37

 

Cypress testing libray를 설치 후 Form 가져오기

describe("로그인", () => {
  it("홈페이지로 이동해야 합니다.", () => {
    cy.visit("/").title().should("eq", "로그인 | Nuber Eats");
  });

  it("Form에 내용을 입력해야 합니다.", () => {
    cy.visit("/")
      .findByPlaceholderText("이메일")
      .type("test@mail.com")
      .findByPlaceholderText("비밀번호")
      .type("123123")
      .findByRole("button")
      .should("not.have.class", "pointer-events-none");
  });
});

 

 

 

Cypress 실행하여 테스트

npx cypress open

 

 

테스트 결과 : 에러 - 체인으로 연결되지 않았음

  • react tesing library - 계속 연결하는 걸 허용하지 않음 - 분리가 필요
    • findBy로 시작하는 커스텀 쿼리 함수는 체인 방식으로 작동하지 않음
    • findByPlaceholderText와 같은 findBy 메서드는 React Testing Library에서 제공하는 비동기 쿼리 메서드이다.
    • 이유는 Cypress의 findBy 메서드가 기본적으로 Promise 객체를 반환한다.
    • Cypress는 비동기 방식으로 DOM을 조회하고 상태를 확인하는 특성을 가지고 있기 때문이다.
    • findByPlaceholderText를 사용할 때, 또 다른 cy로 분리해야 한다.

 

 

에러 해결 방법 01 : cy 분리하여 작성

describe("로그인", () => {
  it("홈페이지로 이동해야 합니다.", () => {
    cy.visit("/").title().should("eq", "로그인 | Nuber Eats");
  });

  it("Form에 내용을 입력해야 합니다.", () => {
    cy.visit("/");
    cy.findByPlaceholderText("이메일").type("test@mail.com");
    cy.findByPlaceholderText("비밀번호").type("123123");
    cy.findByRole("button").should("not.have.class", "pointer-events-none");
  });

  it("이메일, 비밀번호 검증의 오류를 표시해야 합니다.", () => {
    cy.visit("/");
    cy.findByPlaceholderText("이메일").type("bad@email");
    cy.findByRole("alert").should("have.text", "잘못된 이메일 형식입니다.");
  });
});
  • .type은 체인 가능

 

 

cy를 변수명으로 교체

describe("로그인", () => {
  const user = cy;
  
  it("홈페이지로 이동해야 합니다.", () => {
    user.visit("/").title().should("eq", "로그인 | Nuber Eats");
  });

  it("Form에 내용을 입력해야 합니다.", () => {
    user.visit("/");
    user.findByPlaceholderText("이메일").type("test@mail.com");
    user.findByPlaceholderText("비밀번호").type("123123");
    user.findByRole("button").should("not.have.class", "pointer-events-none");
  });

  it("이메일, 비밀번호 검증의 오류를 표시해야 합니다.", () => {
    user.visit("/");
    user.findByPlaceholderText("이메일").type("bad@email");
    user.findByRole("alert").should("have.text", "잘못된 이메일 형식입니다.");
  });
});

보기 좋게 하려는 의도

 

 

해결방법 02 : 체인으로 사용하고 싶다면 findBy를 쓰는 대신 get 사용

describe("로그인", () => {
  const user = cy;

  it("홈페이지로 이동해야 합니다.", () => {
    user.visit("/").title().should("eq", "로그인 | Nuber Eats");
  });

  it("Form에 내용을 입력해야 합니다.", () => {
    user
      .visit("/")
      .get("input[placeholder='이메일']")
      .type("test@mail.com")
      .get("input[placeholder='비밀번호']")
      .type("123123")
      .get("button")
      .should("not.have.class", "pointer-events-none");
  });

  it("이메일, 비밀번호 검증의 오류를 표시해야 합니다.", () => {
    user
      .visit("/")
      .get("input[placeholder='이메일']")
      .type("bad@email")
      .get('[role="alert"]')
      .should("have.text", "잘못된 이메일 형식입니다.");
  });
});
  • Cypress 기본 메서드 cy.get()을사용하는 것이 좋다.
  • 비동기적인 메서드들에 대해 .then() 을 사용하여 후속 작업을 수행할 수 있다.
  • Cypress 기본 메서드 cy.get()은 DOM을 찾아 해당 요소에 대한 명령어를 체인 방식으로 사용할 수 있게 해 준다.
  • 이 경우, CSS 선택자나 텍스트를 기반으로 요소를 찾는다.

 

 

저작자표시 변경금지 (새창열림)

'Frontend > Cypress' 카테고리의 다른 글

Custom Commands  (0) 2024.12.02
Intercept() : cypress를 이용해 request를 가로채는 법  (0) 2024.11.29
Cypress, 브라우저의 localStorage에 저장된 값을 검증하는 코드 테스트  (0) 2024.11.25
Cypress testing libray 설치  (0) 2024.11.14
Cypress, 실제 사용 환경에 가까운 테스트  (0) 2024.11.14
    'Frontend/Cypress' 카테고리의 다른 글
    • Intercept() : cypress를 이용해 request를 가로채는 법
    • Cypress, 브라우저의 localStorage에 저장된 값을 검증하는 코드 테스트
    • Cypress testing libray 설치
    • Cypress, 실제 사용 환경에 가까운 테스트
    wam
    wam

    티스토리툴바