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
JSDC2020 - 用 API mocking 讓前端不再苦等待
Search
Huli
October 17, 2020
Programming
0
1.2k
JSDC2020 - 用 API mocking 讓前端不再苦等待
https://github.com/aszx87410/api-mocking-demo-app
簡單介紹 MSW 與 MirageJS
Huli
October 17, 2020
Tweet
Share
More Decks by Huli
See All by Huli
ModernWeb 2018 - 輕鬆應付複雜的非同步操作:RxJS + Redux Observable
aszx87410
0
150
Attacking web without JS - CSS injection
aszx87410
0
3.1k
Front-end Security that Front-end developers don't know
aszx87410
2
5.7k
你懂了 JavaScript,也不懂 JavaScript - MOPCON 2021
aszx87410
3
970
接觸資安才發現前端的水真深 - Modern Web 2021
aszx87410
0
2.2k
Other Decks in Programming
See All in Programming
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
400
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
760
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
260
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
220
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
110
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
8
1.8k
GitHubで育つ コラボレーション文化 : ニフティでのインナーソース挑戦事例 - 2024-12-16 GitHub Universe 2024 Recap in ZOZO
niftycorp
PRO
0
1.1k
情報漏洩させないための設計
kubotak
5
1.2k
선언형 UI에서의 상태관리
l2hyunwoo
0
250
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
250
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
1k
快速入門可觀測性
blueswen
0
480
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
How GitHub (no longer) Works
holman
312
140k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Navigating Team Friction
lara
183
15k
Optimising Largest Contentful Paint
csswizardry
33
3k
The Cost Of JavaScript in 2023
addyosmani
46
7.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
97
17k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Building Adaptive Systems
keathley
38
2.3k
Done Done
chrislema
182
16k
Transcript
⽤用 API mocking 讓前 端不再苦苦等待 huli
[email protected]
https://blog.huli.tw OneDegree 資深前端⼯工程師
前端 後端 API User
前端 後端 API User
前端 後端 API User 「我要等後端好才能開始接喔」
該怎麼處理理這個狀狀況?
如果後端連 interface 都還沒定好...
http://www.quickmeme.com/meme/3rwr8s
None
幾個⽅方法 • 不管,繼續等待後端
幾個⽅方法 • 不管,繼續等待後端 • Express ⾃自⼰己弄弄
幾個⽅方法 • 不管,繼續等待後端 • Express ⾃自⼰己弄弄 • JSON server 假資料
幾個⽅方法 • 不管,繼續等待後端 • Express ⾃自⼰己弄弄 • JSON server 假資料
• MSW • MirageJS
None
None
登入⾴頁⾯面 後台⾴頁⾯面 是否有 token 是 POST /login 成功之後把 token 寫進
localStorage 並導到後台 否,輸入帳號密碼
登入⾴頁⾯面 確認 localStorage 後台⾴頁⾯面 GET /me 登入⾴頁⾯面 沒 token 有
token 失敗 是否有 token 是 POST /login 成功之後把 token 寫進 localStorage 並導到後台 否,輸入帳號密碼 清除 localStorage
MSW Mock Service Worker
None
前端網站 API Server
前端網站 Service Worker API Server 瀏覽器
設定步驟 • 加上 service worker • 加上 handler
None
None
None
None
還可以針對各種狀狀況 寫測試!
None
None
None
Mirage JS
None
前端網站 API Server
前端網站 fetch XHR API Server JavaScript
None
None
None
寫測試? 當然沒問題
None
None
mirage 的強項
None
None
None
None
總結 cy.route msw mirage 使⽤用在開發上 X O O 使⽤用在測試上 O
O O 特⾊色 cypress 內建 使⽤用 service worker 打造出完整的 mock api 缺點 只能⽤用在測試 設置較⿇麻煩 適合放靜態資料 需要較多時間
https://github.com/aszx87410/api- mocking-demo-app