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

인기 글

태그

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

최근 글

관리자

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

w__am 개발노트

Custom Commands
Frontend/Cypress

Custom Commands

2024. 12. 2. 19:40

 

로그인 되었는지 commands로 만들기

 

 

토큰 확인하는 부분 command 로 만들기

// cypress/integration/auth/create-account.cy.ts

  it("계정을 생성하고 로그인해야 합니다.", () => {
   ...

    // 계정 생성
   ...

    // 만든 계정으로 로그인
    user.wait(2000);
    user.title().should("eq", "로그인 | Nuber Eats");
    user.findByPlaceholderText("이메일").type("testClient16@mail.com");
    user.findByPlaceholderText("비밀번호").type("123123");
    user.findByRole("button").click();
    user.window().its("localStorage.nuber-token").should("be.a", "string");
  });

 

 

// cypress/support/commands.ts

Cypress.Commands.add("assertLoggedIn", () => {
  cy.window().its("localStorage.nuber-token").should("be.a", "string");
});
  • support/commands.js에서 나만의 command를 만들 수 있다.
  • 로그인 되었는지를 확인하는 작업을 반복한다면 custom commands에 만들어 주면 된다.
  • assertLoggedIn 네임명은 임의로 만들어주면 된다.
  • Cypress에는 사용자 지정 명령을 만들고 기존 명령을 덮어쓸 수 있는 자체 API가 함께 제공된다.

 

 

command에서 토큰 확인하는 부분 불러오기

// cypress/integration/auth/create-account.cy.ts

it("계정을 생성하고 로그인해야 합니다.", () => {
   ...

    // 계정 생성
   ...

    // 만든 계정으로 로그인
    user.wait(2000);
    user.title().should("eq", "로그인 | Nuber Eats");
    user.findByPlaceholderText("이메일").type("testClient16@mail.com");
    user.findByPlaceholderText("비밀번호").type("123123");
    user.findByRole("button").click();
    user.assertLoggedIn();
  });

 

 

에러 확인

Argument of type '"assertLoggedIn"' is not assignable to parameter of type 'keyof Chainable<any>'.ts(2345)
  • 이 오류는 TypeScript에서 Cypress.Commands.add의 첫 번째 인자가 Cypress의 기존 명령 체인에 없는 것으로 간주되기 때문에 발생한다.
  • TypeScript는 keyof Chainable<any>로 타입을 검사하며, assertLoggedIn이 해당 타입에 정의되지 않았기 때문이다.
  • 이를 해결하려면 사용자 정의 명령어를 TypeScript에서 Cypress의 명령어 체인에 추가해야 한다.

 

 

1. cypress.d.ts 파일 수정

// cypress/support/cypress.d.ts

declare namespace Cypress {
  interface Chainable<Subject> {
    /**
     * Custom command to assert the user is logged in.
     * @example cy.assertLoggedIn()
     */
    assertLoggedIn(): Chainable<Subject>;
  }
}

cypress/support 디렉토리 아래에 cypress.d.ts 파일을 추가하거나 기존 파일이 있으면 수정한다. 이 파일에서 Cypress 명령 체인을 확장할 수 있다.

 

 

2. tsconfig.json 파일 확인

{
  "compilerOptions": {
    "types": ["cypress"]
  },
  "include": [
    "cypress/**/*.ts"
  ]
}

Cypress 타입 정의를 인식하도록 tsconfig.json에 types 설정을 추가해야 한다.

 

 

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

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

fixture, 테스트에 사용될 미리 정의된 정적 데이터 파일  (0) 2024.12.02
Intercept() : cypress를 이용해 request를 가로채는 법  (0) 2024.11.29
Cypress, 브라우저의 localStorage에 저장된 값을 검증하는 코드 테스트  (0) 2024.11.25
Cypress, React Testing Library에서 제공하는 비동기 쿼리 메서드 체인 안되는 부분 해결 방법  (0) 2024.11.25
Cypress testing libray 설치  (0) 2024.11.14
    'Frontend/Cypress' 카테고리의 다른 글
    • fixture, 테스트에 사용될 미리 정의된 정적 데이터 파일
    • Intercept() : cypress를 이용해 request를 가로채는 법
    • Cypress, 브라우저의 localStorage에 저장된 값을 검증하는 코드 테스트
    • Cypress, React Testing Library에서 제공하는 비동기 쿼리 메서드 체인 안되는 부분 해결 방법
    wam
    wam

    티스토리툴바