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
1.4k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JSDC2020 - 用 API mocking 讓前端不再苦等待
https://github.com/aszx87410/api-mocking-demo-app
簡單介紹 MSW 與 MirageJS
Huli
October 17, 2020
More Decks by Huli
See All by Huli
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
4
4.1k
ModernWeb 2018 - 輕鬆應付複雜的非同步操作:RxJS + Redux Observable
aszx87410
0
310
Attacking web without JS - CSS injection
aszx87410
0
3.5k
Front-end Security that Front-end developers don't know
aszx87410
2
6k
你懂了 JavaScript,也不懂 JavaScript - MOPCON 2021
aszx87410
3
1.3k
接觸資安才發現前端的水真深 - Modern Web 2021
aszx87410
0
2.6k
Other Decks in Programming
See All in Programming
バグを直したら useEffect が消えた
colorful12
3
830
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
0
120
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
130
型解析で実現する Go の言語内 DSL / Conference に Go! タイムテーブルの歩き方 for Gophers
mazrean
0
120
T3DD26: From RAGs to Riches
martinhelmich
0
140
What We Talk About When We Talk About XP
m_seki
2
320
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
620
Foundry Localでエージェント開発
seosoft
0
120
高専キャリア LT 発表内容
crysta1221
6
5.5k
新卒PdEのリアル
ryu1013
1
280
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
不幸な GC
chencmd
0
870
Featured
See All Featured
The Language of Interfaces
destraynor
162
27k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
890
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
500
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
410
Embracing the Ebb and Flow
colly
88
5.2k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.9k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
250
Product Roadmaps are Hard
iamctodd
55
13k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
270
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
320
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
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