Upgrade to Pro — share decks privately, control downloads, hide ads and more …

腐らないUIテストのためのStorybook + Storyshots / #react_ky...

腐らないUIテストのためのStorybook + Storyshots / #react_kyoto v0.3.0

React.kyoto v0.3.0 ( https://react-kyoto.connpass.com/event/137847/ )でStorybookとStoryshotsを使ったコンポーネントのUIテストについて話しました。
質問・不備・マサカリ等はTwitterにてメンションつけてもらえると嬉しいです。
Twitter: https://twitter.com/shisama_

Masashi Hirano

July 19, 2019
Tweet

More Decks by Masashi Hirano

Other Decks in Programming

Transcript

  1. import React from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

    import { faHeart } from '@fortawesome/free-regular-svg-icons'; type Props = { count: number; onClick: React.MouseEventHandler; }; export const Like = ({ count, onClick }: Props) => { return ( <> <div onClick={onClick}> <FontAwesomeIcon icon={faHeart} /> {count} </div> </> ); }; e.g. Likeίϯϙʔωϯτ
  2. @storybook/cli • npx -p @storybook/cli sb init --type react •

    StorybookͷઃఆΛࣗಈͰߦͬͯ͘ΕΔCLI • npm-scripts௥Ճ • StorybookͷઃఆϑΝΠϧ࡞੒ • Storybookͷαϯϓϧίʔυ࡞੒
  3. module.exports = ({ config }) => { config.module.rules.push({ test: /\.tsx?$/,

    use: [ { loader: require.resolve('ts-loader') }, { loader: require.resolve('react-docgen-typescript-loader') } ] }); config.resolve.extensions.push('.ts', '.tsx'); return config; }; .storybook/webpack.config.js OQNJ%UTMPBEFS OQNJ%SFBDU EPDHFOUZQFTDSJQU
  4. import { configure } from '@storybook/react'; // ͜ͷrequire.contextͰಡΈࠐΉର৅ͷ֦ுࢠΛ.js͔Β.tsxʹฤू // const

    req = require.context('../stories', true, /\.stories\.js?$/); const req = require.context('../stories', true, /\.stories\.tsx?$/); function loadStories() { req.keys().forEach(filename => req(filename)); } configure(loadStories, module); .storybook/config.js ॳظ஋Ͱ͸KTΛࢦఆ͍ͯ͠Δ ͷͰUTY ͳͲʹมߋ
  5. import React from "react"; import { storiesOf } from "@storybook/react";

    import { Like } from "../src/components/Like"; storiesOf("Like", module).add("0", () => ( <Like count={0} onClick={() => {}} /> )).add("1", () => ( <Like count={1} onClick={() => {}} /> )); Storybook༻ͷίʔυΛॻ͘ FHDPVOU͕AAͱAAͷ৔߹ ͷTUPSJFTΛ௥Ճ͢Δ
  6. Babelͷઃఆ { "presets": [ [ "@babel/preset-env", { "targets": { "node":

    "current" } } ], "@babel/preset-typescript", "@babel/preset-react" ], "env": { "test": { "plugins": ["require-context-hook"] } } } KFTUDPOpHKTͷઃఆϑΝΠ ϧͷͨΊʹQSFTFUFOWΛΠ ϯετʔϧ UFTUͷͱ͖͚ͩSFRVJSF DPOUFYUIPPL͕ݺ͹ΕΔΑ ͏ʹ͓ͯ͘͠