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.3k
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
200
Attacking web without JS - CSS injection
aszx87410
0
3.2k
Front-end Security that Front-end developers don't know
aszx87410
2
5.8k
你懂了 JavaScript,也不懂 JavaScript - MOPCON 2021
aszx87410
3
1.1k
接觸資安才發現前端的水真深 - Modern Web 2021
aszx87410
0
2.4k
Other Decks in Programming
See All in Programming
decksh - a little language for decks
ajstarks
4
21k
副作用と戦う PHP リファクタリング ─ ドメインイベントでビジネスロジックを解きほぐす
kajitack
3
520
AIコーディングエージェント全社導入とセキュリティ対策
hikaruegashira
16
9.4k
Quality Gates in the Age of Agentic Coding
helmedeiros
PRO
1
120
Go製CLIツールをnpmで配布するには
syumai
2
1.1k
GUI操作LLMの最新動向: UI-TARSと関連論文紹介
kfujikawa
0
430
書き捨てではなく継続開発可能なコードをAIコーディングエージェントで書くために意識していること
shuyakinjo
0
200
リバースエンジニアリング新時代へ! GhidraとClaude DesktopをMCPで繋ぐ/findy202507
tkmru
7
1.7k
ソフトウェア設計とAI技術の活用
masuda220
PRO
25
7.3k
DynamoDBは怖くない!〜テーブル設計の勘所とテスト戦略〜
hyamazaki
0
180
新しいモバイルアプリ勉強会(仮)について
uetyo
1
250
GitHub Copilotの全体像と活用のヒント AI駆動開発の最初の一歩
74th
6
1.8k
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
173
14k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Being A Developer After 40
akosma
90
590k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.3k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
The Straight Up "How To Draw Better" Workshop
denniskardys
235
140k
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