最初發布:souvikinator.xyz
以下是如何為 API 端點設定單元測試。
我們將使用以下 NPM 套件:
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" } }
一個快速伺服器,在 /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中文網其他相關文章!