【发布时间】:2020-07-04 13:05:25
【问题描述】:
我正在尝试为我的 Express 服务器中需要连接到我的 MongoDB 数据库的各种路由设置测试。
我不确定如何构建 Jest 文件以进行测试。在我的普通 index.js 文件中,我正在导入应用程序,并在 connect .then 调用中运行 app.listen,如下所示:
const connect = require("../dbs/mongodb/connect");
connect()
.then(_ => {
app.listen(process.env.PORT, _ => logger.info('this is running')
})
.catch(_ => logger.error('The app could not connect.');
我尝试在我的 test.js 文件中运行相同的设置,但它不起作用。
例如:
const connect = require("../dbs/mongodb/connect");
const request = require("supertest");
const runTests = () => {
describe("Test the home page", () => {
test("It should give a 200 response.", async () => {
let res = await request(app).get("/");
expect(res.statusCode).toBe(200);
});
});
};
connect()
.then(_ => app.listen(process.env.PORT))
.then(runTests)
.catch(err => {
console.error(`Could not connect to mongodb`, err);
});
如何在运行测试之前等待与 MongoDB 的连接?
【问题讨论】:
标签: javascript mongodb express jestjs supertest