如何使用 mocha 测试我的 express/typeorm 应用程序?
How can I test my express/typeorm app with mocha?
这是我的index.ts
import "reflect-metadata";
import {createConnection, Server} from "typeorm";
import express from "express";
import * as bodyParser from "body-parser";
import routes from "./routes/routes";
import cors from 'cors';
const init = () => createConnection().then( async () => {
const app = express();
// create express app
app.use(bodyParser.json());
app.use(cors());
// register express routes from defined application routes
app.use("/", routes);
app.listen(3000);
console.log("Express server has started on port 3000.");
return app;
}).catch(error => console.log(error));
export default init;
我想在我的测试中导入 init,
import chai from 'chai';
import chaiHttp from 'chai-http';
import init from '..';
chai.use(chaiHttp);
chai.should();
let app;
describe("TESTS", () => {
before(async () => {
app = await init();
});
describe("GET /posts", () => {
//Test to get all posts
it("Should get all posts", (done) => {
chai.request(app)
.get('/posts')
.end((err, response) => {
response.should.have.status(200);
response.body.should.be.a('object');
done();
});
});
});
});
此代码有效,但我想在测试结束时关闭连接 server.close
(服务器是 app.listen() 的 returned 对象)但是当我尝试类似
的操作时,我不知道如何导出该对象
return {app: app, server: server}
当我尝试在我的测试中使用它时出现错误。
Property 'app' does not exist on type 'void | { app: Express; server: Server; }'.
我尝试指定 return 类型的 init() 但我收到错误...我想我不知道该怎么做。
我解决了。
谢谢你@Aluan Haddad你指引了我正确的道路。
我在函数内部使用 try catch 并更改了一些小东西。
const init = createConnection().then( async () => {
try {
const app = express();
// create express app
app.use(bodyParser.json());
app.use(cors());
// register express routes from defined application routes
app.use("/", routes);
const server = app.listen(3000);
console.log("Express server has started on port 3000.");
return {app, server};
} catch (e) {
console.log(e);
}
});
describe("TESTS", () => {
before(async function() {
console.log("Before executed")
const appData = await init;
app = appData.app;
server = appData.server;
});
after(() => {
server.close();
console.log("After executed")
});
}
如果我做错了什么请告诉我
这是我的index.ts
import "reflect-metadata";
import {createConnection, Server} from "typeorm";
import express from "express";
import * as bodyParser from "body-parser";
import routes from "./routes/routes";
import cors from 'cors';
const init = () => createConnection().then( async () => {
const app = express();
// create express app
app.use(bodyParser.json());
app.use(cors());
// register express routes from defined application routes
app.use("/", routes);
app.listen(3000);
console.log("Express server has started on port 3000.");
return app;
}).catch(error => console.log(error));
export default init;
我想在我的测试中导入 init,
import chai from 'chai';
import chaiHttp from 'chai-http';
import init from '..';
chai.use(chaiHttp);
chai.should();
let app;
describe("TESTS", () => {
before(async () => {
app = await init();
});
describe("GET /posts", () => {
//Test to get all posts
it("Should get all posts", (done) => {
chai.request(app)
.get('/posts')
.end((err, response) => {
response.should.have.status(200);
response.body.should.be.a('object');
done();
});
});
});
});
此代码有效,但我想在测试结束时关闭连接 server.close
(服务器是 app.listen() 的 returned 对象)但是当我尝试类似
return {app: app, server: server}
当我尝试在我的测试中使用它时出现错误。
Property 'app' does not exist on type 'void | { app: Express; server: Server; }'.
我尝试指定 return 类型的 init() 但我收到错误...我想我不知道该怎么做。
我解决了。 谢谢你@Aluan Haddad你指引了我正确的道路。
我在函数内部使用 try catch 并更改了一些小东西。
const init = createConnection().then( async () => {
try {
const app = express();
// create express app
app.use(bodyParser.json());
app.use(cors());
// register express routes from defined application routes
app.use("/", routes);
const server = app.listen(3000);
console.log("Express server has started on port 3000.");
return {app, server};
} catch (e) {
console.log(e);
}
});
describe("TESTS", () => {
before(async function() {
console.log("Before executed")
const appData = await init;
app = appData.app;
server = appData.server;
});
after(() => {
server.close();
console.log("After executed")
});
}
如果我做错了什么请告诉我