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
140
「バイトル」のTypeScriptリニューアル — 積み上がったレガシーとパフォーマンスに挑む現在地
hayato_yokoyama
0
100
AIが特別じゃなくなった時代に、作ることを楽しもう
hayato_yokoyama
0
38
AIのためのテスト戦略 〜TDDが難しいフロントエンド開発でのアプローチ〜
hayato_yokoyama
0
270
実はすごいスピードで進化しているCSS
hayato_yokoyama
0
260
Next.js AppRouter × GraphQL 〜 夢見た理想と現実の課題 〜
hayato_yokoyama
0
180
Other Decks in Programming
See All in Programming
AI に Inclusive UI を書かせよう — Design Rules Skill で Compose UI を作り直す
theoriatec2024
1
470
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
300
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
280
AgentCore CLI で進化した AWS での AI エージェントの作り方 : 必要な機能を必要な時に
icoxfog417
PRO
2
270
[DroidKaigi 2026] Bring your own phones to Gradle Managed Devices
f2lk
0
110
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
430
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
55
37k
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
130
新卒PdEのリアル
ryu1013
1
480
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
3
510
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
7
3.5k
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
72
12k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Paper Plane (Part 1)
katiecoart
PRO
1
11k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Speed Design
sergeychernyshev
33
2.1k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.2k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
440
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.2k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
890
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 …
ありがとうございました!!!!