最初发布: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中文网其他相关文章!