300x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- prettier 자동화
- chromatic
- 자바스크립트
- storybook
- react
- 우테코
- string-width
- 성능 베이스캠프
- CSS
- 협업
- 유틸함수
- import정리
- 우아한테크코스
- Husky
- 크로마틱
- vscode
- JavaScript
- IDL attributes
- 이슈번호자동화
- 프로젝트
- 카페인
- eslint에러 자동fix
- 클린코드
- importOrder
- 프로젝트 카페인
- css instead of js
- eslint 자동화
- git hooks
- webpack
- 클로저
Archives
- Today
- Total
FEB:)DAIN
storybook에서 msw 사용하기 본문
728x90

MSW란?
Mock Service Worker의 약자로 API를 Mocking 할 때 사용하는 도구
API 요청을 가로채는 Service Worker 기능을 사용하여 mocking 할 수 있다.
로컬 서버는 key를 입력해서 들어가야 하는 등 번거로움이 있어 스토리북을 통해 개발을 하고 있었다.
그러던 중 API mocking이 필요해졌고, msw와 스토리북을 연동하기 위해 msw-storybook-addon을 설치했다.
npm i msw-storybook-addon -D
// or
yarn add msw-storybook-addon -D
하지만 스토리북에서 msw가 잘 작동하지 않았다.🥲
TypeError: Failed to update a ServiceWorker for scope ('http://localhost:6006/') with script ('http://localhost:6006/mockServiceWorker.js'): A bad HTTP response code (404) was received when fetching the script.
404 NOT FOUND와 함께 저런 에러 메시지가 등장했다. msw가 작동하지도 않았다.
만약 똑같은 문제를 만났다면 아래의 설명대로 하면 해결된다.
1) .storybook/preview.tsx에 `msw-storybook-addon`의 initialize, mswDecorator 사용
// preview.tsx
import { initialize, mswDecorator } from 'msw-storybook-addon';
initialize();
const preview: Preview = {
parameters: {
...
},
decorators: [
(Story) => (
<>
<GlobalStyle />
<Story />
</>
),
mswDecorator,
],
}
2) parameters에 msw 추가
// preview.tsx
import { handlers } from '../src/mocks/handlers';
const preview: Preview = {
parameters: {
...
msw: {
handlers: [...handlers],
},
},
...
}
3) .storybook/main.ts에 `staticDirs: ['../public']` 추가
// main.ts
const config: StorybookConfig = {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
staticDirs: ['../public'], // ⭐public 경로 추가!
...
}
이렇게 하면 성공적으로 스토리북과 msw가 연동된다. (나는 3번을 빼먹어서 오류가 난 것이었다...)


728x90
'코딩 > 트러블 슈팅' 카테고리의 다른 글
Manifest: Line: 1, column: 1, Syntax error (0) | 2023.09.11 |
---|---|
[스토리북 에러] var stringWidth = require('string-width') Error [ERR_REQUIRE_ESM] (0) | 2023.08.10 |
storybook에서 svg 오류 개선 (0) | 2023.07.25 |
git hooks(커밋 메시지에 이슈 번호 자동 입력)와 husky 충돌 개선 (0) | 2023.07.20 |
import React from 'react' 생략하는 법 (0) | 2023.07.01 |