使用 Jest 和 Supertest 測試 REST API ✅
最初發布:souvikinator.xyz
以下是如何為 API 端點設定單元測試。
我們將使用以下 NPM 套件:
- 伺服器的Express(可以使用任何其他框架)
- 開玩笑
- 超級測驗
VS Code 擴充使用:
- 開玩笑
- 玩笑跑者
安裝依賴項
npm install express
npm install -D jest supertest
目錄結構如下:
並且不要忘記 package.json
{ "name": "api-unit-testing", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "jest api.test.js", "start":"node server.js" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.18.1" }, "devDependencies": { "jest": "^28.1.2", "supertest": "^6.2.3" } }
創建一個簡單的 Express 伺服器
一個快速伺服器,在 /post 端點上接受帶有正文和標題的 POST 請求。如果其中一個缺失,則會傳回狀態 400 Bad Request。
// app.js const express = require("express"); const app = express(); app.use(express.json()); app.post("/post", (req, res) => { const { title, body } = req.body; if (!title || !body) return res.sendStatus(400).json({ error: "title and body is required" }); return res.sendStatus(200); }); module.exports = app;
const app = require("./app"); const port = 3000; app.listen(port, () => { console.log(`http://localhost:${port} ?`); });
express 應用程式已匯出以與 supertest 一起使用。使用其他 HTTP 伺服器框架也可以完成相同的操作。
建立測試文件
// api.test.js const supertest = require("supertest"); const app = require("./app"); // express app describe("Creating post", () => { // test cases goes here });
Supertest 負責運行伺服器並為我們發出請求,而我們則專注於測試。
express 應用程式被傳遞給超級測試代理,並將express 綁定到隨機可用連接埠。然後我們可以向所需的 API 端點發出 HTTP 請求,並將其回應狀態與我們的期望進行比較:
const response = await supertest.agent(app).post("/post").send({ title: "Awesome post", body: "Awesome post body", });
我們將為 3 個案例建立測試:
- 當標題和正文都提供時
test("creating new post when both title and body are provided", async () => { const response = await supertest.agent(app).post("/post").send({ title: "Awesome post", body: "Awesome post body", }); // comparing response status code with expected status code expect(response.statusCode).toBe(200); });
- 當其中任何一個失蹤時
test("creating new post when either of the data is not provided", async () => { const response = await supertest.agent(app).post("/post").send({ title: "Awesome post", }); expect(response.statusCode).toBe(400); });
- 當他們兩個都失蹤時
test("creating new post when no data is not provided", async () => { const response = await supertest.agent(app).post("/post").send(); expect(response.statusCode).toBe(400); });
最終程式碼應如下圖所示:
const supertest = require("supertest"); const app = require("./app"); // express app describe("Creating post", () => { test("creating new post when both title and body are provided", async () => { const response = await supertest.agent(app).post("/post").send({ title: "Awesome post", body: "Awesome post body", }); expect(response.statusCode).toBe(200); }); test("creating new post when either of the data is not provided", async () => { const response = await supertest.agent(app).post("/post").send({ title: "Awesome post", }); expect(response.statusCode).toBe(400); }); test("creating new post when no data is not provided", async () => { const response = await supertest.agent(app).post("/post").send(); expect(response.statusCode).toBe(400); }); });
運行測試
使用以下命令執行測試:
npm run test
如果使用 VS Code 擴充功能(例如 jest 和 jest runner),那麼它會為您完成工作。
這就是我們如何輕鬆測試 API 端點的方法。 ?
以上是使用 Jest 和 Supertest 測試 REST API ✅的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...
