使用 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...
