Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
フロントエンドテストを書きやすくするために工夫したこと
Search
Hayato Yokoyama
June 11, 2024
Programming
95
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
フロントエンドテストを書きやすくするために工夫したこと
Hayato Yokoyama
June 11, 2024
More Decks by Hayato Yokoyama
See All by Hayato Yokoyama
「型ガードしたのにnullable」から卒業する
hayato_yokoyama
0
150
「バイトル」のTypeScriptリニューアル — 積み上がったレガシーとパフォーマンスに挑む現在地
hayato_yokoyama
0
110
AIが特別じゃなくなった時代に、作ることを楽しもう
hayato_yokoyama
0
38
AIのためのテスト戦略 〜TDDが難しいフロントエンド開発でのアプローチ〜
hayato_yokoyama
0
280
実はすごいスピードで進化しているCSS
hayato_yokoyama
0
260
Next.js AppRouter × GraphQL 〜 夢見た理想と現実の課題 〜
hayato_yokoyama
0
180
Other Decks in Programming
See All in Programming
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.3k
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
180
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
390
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
150
Java 27新機能 / Java 27 new features
kishida
2
180
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
130
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
310
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
8
5.2k
Are APIs Still Relevant in the AI Era?
soyuka
0
370
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
570
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
140
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
220
Featured
See All Featured
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
340
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Producing Creativity
orderedlist
PRO
348
41k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
620
Optimizing for Happiness
mojombo
378
71k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
So, you think you're a good person
axbom
PRO
2
2.2k
A Modern Web Designer's Workflow
chriscoyier
699
190k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Building Adaptive Systems
keathley
44
3.2k
Transcript
フロントエンドテストを 書きやすくするために工夫したこと 2024/06/11 横山 隼 UV Study Lightning Talks #50
自己紹介 • ディップ株式会社 • 2022年新卒入社 • フロントエンドエンジニア • X: @hayatoyokoyama_
• GitHub: @hayato-yokoyama 横山 隼
フロントエンドのテストは書いていますか?
現在、会社の開発チームでは、ページ単位で行う E2Eテストと、 コンポーネント単位で行うユニットテスト の2種類のテストを行っています。 ページ ページ (page.tsx) E2Eテスト コンポーネント ユニットテスト
コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト
現在、会社の開発チームでは、ページ単位で行う E2Eテストと、 コンポーネント単位で行うユニットテスト の2種類のテストを行っています。 ページ ページ (page.tsx) E2Eテスト コンポーネント ユニットテスト
コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト コンポーネント ユニットテスト ここのテストを 書きやすくした話
ユニットテストはコンポーネントとセットで テンプレートから作成する
実現するにあたって Hygen というライブラリを使用しました https://www.hygen.io/
とりあえず見てみよう
None
どのディレクトリに配置するか
使い分けたい camelCase/PascalCase/… もカスタマイズ可能
コンポーネント、テスト、Storybookなどは、 コンポーネント名を与えたら、中身以外の枠組みは共通化できる import { render, screen } from "@testing-library/react"; import
Button from "./button"; describe("Buttonコンポーネント", () => { test("ラベルが正しくレンダリングされること ", () => { render(<Button label="Click Me" onClick={() => {}} />); expect(screen.getByText("Click Me")).toBeInTheDocument(); }); }); type ButtonProps = { label: string; onClick: () => void; type?: "button" | "submit" | "reset"; disabled?: boolean; }; const Button = ({ label, onClick, disabled, type }: ButtonProps) => { return ( <button type={type} onClick={onClick} disabled={disabled}> {label} </button> ); }; export default Button; コンポーネント テスト
コンポーネント、テスト、Storybookなどは、 コンポーネント名を与えたら、中身以外の枠組みは共通化できる import { render, screen } from "@testing-library/react"; import
Button from "./button"; describe("Buttonコンポーネント", () => { test("ラベルが正しくレンダリングされること ", () => { render(<Button label="Click Me" onClick={() => {}} />); expect(screen.getByText("Click Me")).toBeInTheDocument(); }); }); type ButtonProps = { label: string; onClick: () => void; type?: "button" | "submit" | "reset"; disabled?: boolean; }; const Button = ({ label, onClick, disabled, type }: ButtonProps) => { return ( <button type={type} onClick={onClick} disabled={disabled}> {label} </button> ); }; export default Button; コンポーネント テスト
コードを自動生成させることで … • 時間を節約 、コードスタイルの統一化 • 自動的にテストコードも生成させることで、 テストの書き忘れを防ぐ ことができる •
最初からテストを書き始められる状態 なので、心理的なハードルが低い
ちなみに、テンプレート化できるものなら、 言語・フレームワーク、用途を問わず、何でも使用できる
導入方法
導入手順 1. Hygenをインストール 2. プロンプトの設定 3. テンプレートの生成 4. Hygenコマンドの登録
1. Hygenをインストール npm install --save-dev hygen
2. プロンプトの設定 module.exports = [ { type: "input", name: "name",
message: "コンポーネント名を入力してください ", }, { type: "confirm", name: "haveStorybook", message: "Storybookファイルを追加しますか?", default: false, }, ];
3. テンプレートの生成 --- to: app/components/<%= name %>/<%= name %>.test.tsx unless_exists:
true --- import { render, screen } from "@testing-library/react"; import <%= name %> from "./<%= name %>"; test("<%= name %>コンポーネントのテキストがレンダリングされること ", () => { render(<<%= name %> text="テキスト" />); expect(screen.getByText("テキスト")); });
4. Hygenコマンド登録 "scripts": { "dev": "next dev", "build": "next build",
"start": "next start", "test": "jest", "add-component": "hygen component new" // 追加 },
以上
気をつけたほうがいいこと
気をつけたほうがいいこと • 「テンプレートからコンポーネントを作る」というルールを 意識的に徹底する必要がある (仕組みで強制できない) • 冗長なテスト、不要なテストが出来やすくなる
まとめ
まとめ • テンプレート生成ツールを使う ことで、テストがかなり書きやすくなった • 「テンプレートからコンポーネントを作成する」というルールを チームで意識的に徹底する 必要がある
参考リンク・その他 参考リンク • hygen で生成 - 対話形式の Component 雛形 -
テンプレート生成に関する Hygenの代替ライブラリ • Plop • Yeoman …
ありがとうございました!!!!