0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

StorybookをGithub.ioにサブディレクトリを切って公開してMSWを導入したメモ

Posted at

概要

前回はStorybookを公開した。
今回、MSWを使ってAPIの戻り値をモックしたものをStorybookで見せるストーリーを記述。
公開時にmockServiceWorker.jsが404になってしまう問題があったので対処した。

追加したストーリー: my/profile をモック化

この時点のソース

環境

  • @storybook/react: 7.0.17
  • msw: 1.2.1

ソースコード

githubにアップしたときのみ、MSWのオプションにserviceWorkerのURLを設定してあげるのがポイント。(参考)

.storybook/preview.ts
import type { Preview } from "@storybook/react";
import { INITIAL_VIEWPORTS } from "@storybook/addon-viewport";
import { initialize, mswDecorator } from "msw-storybook-addon";
import { handleGetMyProfile } from "../src/services/client/MyProfile/__mock__/msw";

const preview: Preview = {
  parameters: {
    actions: { argTypesRegex: "^on[A-Z].*" },
    controls: {
      matchers: {
        color: /(background|color)$/i,
        date: /Date$/,
      },
    },
    viewport: {
      viewports: INITIAL_VIEWPORTS,
    },
    msw: { handlers: [handleGetMyProfile()] },
  },
};

export default preview;
export const decorators = [mswDecorator];
- const options = {};
+ const options =
+   location.hostname !== "hibohiboo.github.io"
+     ? {}
+     : {
+         serviceWorker: {
+           url: "/RinneCircle/storybook-static/mockServiceWorker.js",
+        },
+       };
initialize(options);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?