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
120
Attacking web without JS - CSS injection
aszx87410
0
2.9k
Front-end Security that Front-end developers don't know
aszx87410
2
5.5k
你懂了 JavaScript,也不懂 JavaScript - MOPCON 2021
aszx87410
3
900
接觸資安才發現前端的水真深 - Modern Web 2021
aszx87410
0
2k
Other Decks in Programming
See All in Programming
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
2
81k
リハビリmruby
kishima
1
160
英語
s_shimotori
1
220
HMSコンペ 11th Solution (team : kansai-kaggler)
t88
1
680
Play Billing Library 7.0.0 変更点まとめ@potatotips#88
kako351
0
160
しくじり先生 Image Matching Challenge 2024 編
goosehaaan
0
810
CSC307 Lecture 08
javiergs
PRO
0
330
CSC307 Lecture 13
javiergs
PRO
0
150
ピグパーティにおけるMongoDB CommunityバージョンからAtlasへの移行事例
10969hotaka
0
130
Folding Cheat Sheet #7
philipschwarz
PRO
0
150
Introduction of Happy Eyeballs Version 2 (RFC8305) to the Socket library
coe401_
1
220
はしめてのプログラミングとロボット制御
watawatavoltage
0
290
Featured
See All Featured
Producing Creativity
orderedlist
PRO
340
39k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
17
1.5k
The Cost Of JavaScript in 2023
addyosmani
31
4.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
228
16k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
13
430
Stop Working from a Prison Cell
hatefulcrawdad
266
20k
The World Runs on Bad Software
bkeepers
PRO
63
11k
Rebuilding a faster, lazier Slack
samanthasiow
78
8.5k
Bootstrapping a Software Product
garrettdimon
PRO
304
110k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
20
7.2k
How to train your dragon (web standard)
notwaldorf
79
5.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
121
18k
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