LoginSignup
5
3

More than 5 years have passed since last update.

storybook 上で require.context しつつ storyshots を動かす

Last updated at Posted at 2018-04-19

storyshots は storybook の story を自動で snapshot とってくれるやつです。導入は略。
タイトルの組み合わせだとバグります。ハマった箇所だけ。しょぼいハマりだけど、半年ぐらいわからなかったので…。

src/**/*.stories.js みたいな glob pattern で storybook のコードを評価したい場合、こういうコードになってることが多いんですが

.storybook/config.js
import { configure } from "@storybook/react";

const req = require.context("../src/components", true, /stories\.js$/);
const loadStories = req.keys().forEach(req);

configure(loadStories, module);

これだと loadStories は配列なんですが、storyshots は関数を期待しているので読み込み時に壊れる。そして評価タイミングも実はこれじゃ駄目です。

.storybook/config.js
import { configure } from "@storybook/react";

function loadStories() {
  const req = require.context("../src/components", true, /stories\.js$/);
  req.keys().forEach(req);
}

configure(loadStories, module);

It works.

参考: https://github.com/storybooks/storybook/issues/2894

5
3
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
5
3