Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config/#3: Mock Service Worker 설치 및 설정 #10

Merged
merged 10 commits into from
Jan 21, 2025
Merged

Conversation

Ssechan
Copy link
Collaborator

@Ssechan Ssechan commented Jan 20, 2025

📌 관련 이슈

🔑 주요 변경 사항

  • Msw 설치 및 초기화
    • API요청을 모킹할 핸들러 생성
    • 브라우저 환경 통합을 위한 서비스 워커 생성
    • vitest 등 노드 환경 통합을 위한 server.ts 생성
  • msw 브라우저 환경 작동 확인 및 테스트 환경 작동 확인 진행

📸 스크린 샷 (선택 사항)

📖 참고 사항

- API 요청을 Mocking 할 핸들러 파일 생성
- MSW의 node.js 환경(vitest) 통합을 위한 server 파일 생성
- 테스트 시 MSW 사용을 위한 vitest.setup.ts 파일 수정
- 테스트 전 setup 파일 설정

- ref: #3
- 브라우저 환경 API 모킹을 위한 woker 생성(browser.ts)
- 개발 환경에서 woker 실행을 위한 main.tsx 수정(조건부 msw 시작)
- worker.start()는 비동기 작업으로, 브라우저에 서비스 워커를 등록하는 과정이 포함 필요(.then)

- ref: #3
- tsconfig의 type을 추가하였습니다.
- vitest.config.ts 오타 수정
- vitest.setup.ts에서 CJS인 jest-dom의 matcher를 모두 가져오도록 함.
- @testing-library/user-event" 추가 설치

- ref: #3
vitest.setup.ts Outdated
import { cleanup } from '@testing-library/react';
import matchers from '@testing-library/jest-dom/matchers';
import * as matchers from '@testing-library/jest-dom/matchers';
import { server } from './src/mocks/server';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2 mocks 디렉토리도 절대 경로 별칭으로 등록한 후에, 나머지 상대 경로로 작성한 부분들 수정해 주세요~

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵!

import { setupWorker } from 'msw/browser';
import { handlers } from './handlers';

export const worker = setupWorker(...handlers);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 프로젝트 내에 import/prefer-default-export 규칙이 적용되어 있어요. 해당 규칙을 off 할 건지, 사용할 건지 논의해 보고 나머지 파일에서도 수정이 필요할 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 규칙은 그대로 가져가면 좋을 것 같아요. 규칙 확인에 좀 더 신경쓰겠습니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 tsconfig.eslint.jsonvitest.setup.ts 대신 vitest.workspace.ts이 등록되어 있어요. 이 부분도 수정이 필요할 것 같습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다!!

- mocks 폴더의 절대 경로 별칭을 설정, 상대 경로 수정
- ESLint import/no-extraneous-dependencies 규칙을 off로 바꾸었습니다.
- import/prefer-default-export에 맞게 파일들을 일부 수정했습니다.

- ref: #3
Copy link
Member

@itwillbeoptimal itwillbeoptimal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정상 작동 확인했습니다~ 고생하셨어요

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Config: Mock Service Worker 설치 및 설정
3 participants