express - 如何使用 AVA 测试 Express.js 应用程序?
问题描述
我可以用 AVA 很好地测试我的模型,但我也想测试路线。
我觉得应该可以访问 Express 应用程序对象,并传递一个 URL,然后查看返回的内容,但我不知道如何让 Express 对象使用它。
解决方案
在玩了一些并参考了supertest repo之后,我能够得到以下工作:
const test = require("ava");
const request = require("supertest");
const express = require("express");
test("test express handler with supertest", async t => {
// in async tests, it's useful to declare the number of
// assertions this test contains
t.plan(3);
// define (or import) your express app here
const app = express();
app.get("/", (req, res) => {
res.json({
message: "Hello, World!",
});
});
// make a request with supertest
const res = await request(app).get("/").send();
// make assertions on the response
t.is(res.ok, true);
t.is(res.type, "application/json");
t.like(res.body, {
message: "Hello, World!",
});
});
我倾向于使用以下 shell 命令运行 AVA:
yarn ava --verbose --watch
推荐阅读
- python - 想给一个用户上传几张图片,只允许我上传一张
- jenkins-pipeline - 关于管道的问题 从 Git 签出代码
- ios - ZDCChatView scrollToLast iOS 应用程序崩溃
- azure - 如何使用“联系我”发布选项在 azure 市场中发布 Web 应用程序
- corda - 尝试加载数据时,Corda 节点出现 JVM OutOfMemory 异常
- eonasdan-datetimepicker - Bootstrap DateTimePicker 关闭按钮位置
- java - 文件资源管理器无法启动
- json - Python循环遍历URL中的变量
- java - 在 customAdapter 中删除一行使用 SQLite 扩展 BaseAdapter - Android
- sql - SQL - 如何按项目单位获得最终数量组?