[英]Unhandled promise rejection error while testing with jasmine
The server is working (tested with postman). 服务器正在工作(已通过邮递员测试)。 The code also works if I start the server and I delete the beforeAll and afterAll handlers. 如果启动服务器并删除beforeAll和afterAll处理程序,该代码也将起作用。 The console output is: App listening on port 3200! 控制台输出为:应用程序在端口3200上监听! after server start I don't understand where is the problem. 服务器启动后,我不知道问题出在哪里。
import request from 'request';
import { start, stop } from '../src/server';
describe('A suite', function () {
beforeAll(async () => {
await start();
});
afterAll(async () => {
await stop();
});
it('server test', async () => {
const data = { tableName: 'supply' };
console.log('after server start');
const response = await new Promise((resolve, reject) => {
request.post({
url: 'http://localhost:3200/getTable',
json: data
}, (error, response, body) => {
if (error) {
reject(body);
} else {
resolve(body);
}
});
});
console.log(response);
});
});
const start = async (): Promise<void> => {
await new Promise((resolve, reject) => {
server = app.listen(port, () => {
console.log(`App listening on port ${port}!`);
resolve();
});
});
}
const stop = async (): Promise<void> => {
await new Promise((resolve, reject) => server.close(() => {
console.log('App closed successfully')
resolve();
}));
}
I found out that the problem is that when I run the tests the connection to the database cannot be made. 我发现问题是,当我运行测试时,无法建立与数据库的连接。 Any idea how can I create a connection to the database when I run jasmine? 知道我在运行茉莉花时如何建立与数据库的连接吗? What's the difference between calling start() from the test or just running npm start. 从测试中调用start()或仅运行npm start有什么区别。 When I use npm start everything works fine but when I call it from jasmine the connection to db cannot be made. 当我使用npm start时,一切正常,但是当我从茉莉花调用它时,无法建立与db的连接。
It's a typescript problem, something like the tests are run with node, but they should run with ts-node. 这是一个打字稿问题,类似的测试是通过node运行的,但是它们应该使用ts-node运行。 I run the test with the commands: tsc; 我使用以下命令运行测试:tsc; jasmine. 茉莉花
I solved it using jasmine-ts . 我用茉莉花ts解决了。 the problem was that I ran my tests with tsc; 问题是我使用tsc进行了测试; jasmine 茉莉花
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.