Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
mswでフロントエンドのインテグレーションテストを実装する
Search
Yohei Iino
November 22, 2022
Technology
0
100
mswでフロントエンドのインテグレーションテストを実装する
mswでフロントエンドのインテグレーションテストを実装する
Yohei Iino
November 22, 2022
Tweet
Share
More Decks by Yohei Iino
See All by Yohei Iino
作成中のFlutterアプリの中間発表
wheatandcat
0
16
最近読んだ技術書を簡単紹介
wheatandcat
0
34
ユニバーサルリンク/アプリリンクを使ってQRコードでゲストログインできるようにする
wheatandcat
0
37
Firebase App Checkを実装したので紹介
wheatandcat
0
53
PlanetScaleの無料プランがなくなるので、NeonとTiDBを試してみた
wheatandcat
0
190
Flutter HooksとRiverpodの解説
wheatandcat
0
280
T3 Stack(応用編: Next Auth & SSRの実装紹介)
wheatandcat
1
260
App Routerの紹介
wheatandcat
0
78
Flutter × GraphQLでアプリを作ってみる
wheatandcat
0
240
Other Decks in Technology
See All in Technology
Classmethod AI Talks(CATs) #16 司会進行スライド(2025.02.12) / classmethod-ai-talks-aka-cats_moderator-slides_vol16_2025-02-12
shinyaa31
0
110
Culture Deck
optfit
0
420
白金鉱業Meetup Vol.17_あるデータサイエンティストのデータマネジメントとの向き合い方
brainpadpr
6
760
データ資産をシームレスに伝達するためのイベント駆動型アーキテクチャ
kakehashi
PRO
2
550
2/18/25: Java meets AI: Build LLM-Powered Apps with LangChain4j
edeandrea
PRO
0
120
AndroidデバイスにFTPサーバを建立する
e10dokup
0
250
プロセス改善による品質向上事例
tomasagi
2
2.6k
組織貢献をするフリーランスエンジニアという生き方
n_takehata
1
1.3k
Larkご案内資料
customercloud
PRO
0
650
個人開発から公式機能へ: PlaywrightとRailsをつなげた3年の軌跡
yusukeiwaki
11
3k
人はなぜISUCONに夢中になるのか
kakehashi
PRO
6
1.7k
明日からできる!技術的負債の返済を加速するための実践ガイド~『ホットペッパービューティー』の事例をもとに~
recruitengineers
PRO
3
410
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Docker and Python
trallard
44
3.3k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.1k
4 Signs Your Business is Dying
shpigford
182
22k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Designing for Performance
lara
604
68k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
KATA
mclloyd
29
14k
A better future with KSS
kneath
238
17k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Transcript
msw でフロントエンドの インテグレーションテストを実装する Press Space for next page
自己紹介 📝 飯野陽平(wheatandcat ) 🏢 フリーランスエンジニア(シェアフル株式会社CTO ) 💻 Blog: https://www.wheatandcat.me/
🛠 今までに作ったもの memoir ペペロミア Atomic Design Check List
msw とは msw(Mock Service Worker) は、ネットワークレベルでAPI リクエストをインターセプトしてmock のデータ を返すためのライブラリ Service
Worker を使用しているので、別プロセスでローカルのサーバーを立ち上げる必要が無く手軽に利 用できる フロントエンドのテストコードやStroybook などでも利用可能 デフォルトでRESTful API とGraphQL をサポートしている msw Service Worker とは msw Choose an API
インテグレーションテストとは? フロントエンドのテストは主に以下の4 つに分 類される End to End Integration Unit Static
上位レイヤーに行くほど以下の特徴がある フィードバックの速度は遅い 実装コストは高い 信頼性は向上する The Testing Trophy and Testing Classifications
インテグレーションテストのこれまで ① msw 導入前だと各API をjest のmock 機能を使用してAPI のコールメソッドを、すべてmock させて実装させて いく必要があった
コード的には次のようになっていた
インテグレーションテストのこれまで ② describe("components/pages/ItemDetail/Connected.tsx", () => { jest.spyOn(queries, "useItemQuery").mockImplementation((): any =>
({ loading: false, data: { item: { id: "1", title: " 本を読む", categoryID: 2, date: "2021-01-01T00:00:00+09:00", }, }, error: undefined, refetch: jest.fn(), })); const wrapper = shallow(<Connected {...propsData()} />); it(" 正常にrender すること", () => { expect(wrapper).toMatchSnapshot(); }); });
インテグレーションテストのこれまで ② jest.spyOn(queries, "useItemQuery").mockImplementation((): any => ({ loading: false, data:
{ item: { id: "1", title: " 本を読む", categoryID: 2, date: "2021-01-01T00:00:00+09:00", }, }, error: undefined, refetch: jest.fn(), })); describe("components/pages/ItemDetail/Connected.tsx", () => { const wrapper = shallow(<Connected {...propsData()} />); it(" 正常にrender すること", () => { expect(wrapper).toMatchSnapshot(); }); });
インテグレーションテストのこれまで ② describe("components/pages/ItemDetail/Connected.tsx", () => { jest.spyOn(queries, "useItemQuery").mockImplementation((): any =>
({ loading: false, data: { item: { id: "1", title: " 本を読む", categoryID: 2, date: "2021-01-01T00:00:00+09:00", }, }, error: undefined, refetch: jest.fn(), })); const wrapper = shallow(<Connected {...propsData()} />); it(" 正常にrender すること", () => { expect(wrapper).toMatchSnapshot(); }); });
モチベーション 今までmock 作業で実装に時間の掛かったインテグレーションテストを簡単に実装できる状態にしたい また、実際の動作に伴ったシナリオテストを実装できる状態にしたい
実装に使用したライブラリ msw typed-document-node graphql-codegen-typescript-mock-data testing-library/react-native
実装PR typed-document-node への移行 msw を導入してGraphQL をモックしたテストコードを書く① msw を導入してGraphQL をモックしたテストコードを書く②
実装してみた ① まずは、typescript-react-apollo →typed-document-node 移行を実装 PR: typed-document-node への移行 実装の紹介 こちら、次のPR
で使用するgraphql-codegen-typescript-mock-data を使用しやすくするために移行 typed-document-node は特定のGraphQL クライアントに依存せずにGraphQL のtype を自動生成してくれる codegen のプラグイン
実装してみた ② 次に、msw とgraphql-codegen-typescript-mock-data を導入 PR: msw を導入してGraphQL をモックしたテストコードを書く① 実装の紹介
graphql-codegen-typescript-mock-data は、GraphQL のSchema 情報からダミーデータを生成してくれるライ ブラリ これを利用することでmock データを保守を自動生成で補うようにする 参考コード msw は以下の部分でテスト時に起動させる 参考コード テストは以下の通りに記載 参考コード
実装してみた ③ 最後に、前のPR からの追加で、より正確なシナリオテストを実装 PR: msw を導入してGraphQL をモックしたテストコードを書く② 実装の紹介 アイテムの更新のシナリオテストを追加
参考コード testing-library/react-native を使用して各入力を設定 参考コード 入力に設定した値と更新のAPI 時に設定された値とGraphQL の variables を比較して一致しているかテスト 参考コード
まとめ msw を使えば気軽にインテグレーションテストを書けて、かなり良い。 graphql-codegen 周りの自動生成周りのツールと相性が良い テストから仕様が把握できるコードにしやすくなった
ご清聴ありがとうございました