gpt4 book ai didi

node.js - Jest 测试通过但最后出现错误 : connect ECONNREFUSED 127. 0.0.1:80

转载 作者:行者123 更新时间:2023-11-28 21:35:33 24 4
gpt4 key购买 nike

我在后端使用带有 TypeScript 的 Node ,并在后端使用 Jest 和 Supertest 作为我的测试框架。

当我尝试测试时,我的结果通过了,但最后却出现错误。结果如下:

 PASS  test/controllers/user.controller.test.ts
Get all users
✓ should return status code 200 (25ms)

console.log node_modules/@overnightjs/logger/lib/Logger.js:173
[2019-12-05T04:54:26.811Z]: Setting up database ...

Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.284s
Ran all test suites.
server/test/controllers/user.controller.test.ts:32
throw err;
^

Error: connect ECONNREFUSED 127.0.0.1:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1104:14)
npm ERR! Test failed. See above for more details.

这是我的测试代码:

import request from "supertest";
import { AppServer } from '../../config/server';

const server = new AppServer();

describe('Get all users', () => {
it('should return status code 200', async () => {
server.startDB();
const appInstance = server.appInstance;
const req = request(appInstance);
req.get('api/v1/users/')
.expect(200)
.end((err, res) => {
if (err) throw err;
})
})
})

这是我的服务器设置。我在后端使用 overnightjs

我创建了一个 getter 来获取 Express 实例。这是来自 night.js。

// this should be the very top, should be called before the controllers
require('dotenv').config();

import 'reflect-metadata';

import { Server } from '@overnightjs/core';
import { Logger } from '@overnightjs/logger';
import { createConnection } from 'typeorm';
import helmet from 'helmet';
import * as bodyParser from 'body-parser';
import * as controllers from '../src/controllers/controller_imports';

export class AppServer extends Server {
constructor() {
super(process.env.NODE_ENV === 'development');
this.app.use(helmet());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));
this.setupControllers();
}

get appInstance(): any {
return this.app;
}

private setupControllers(): void {
const controllerInstances = [];

// eslint-disable-next-line
for (const name of Object.keys(controllers)) {
const Controller = (controllers as any)[name];
if (typeof Controller === 'function') {
controllerInstances.push(new Controller());
}
}

/* You can add option router as second argument */
super.addControllers(controllerInstances);
}

private startServer(portNum?: number): void {
const port = portNum || 8000;
this.app.listen(port, () => {
Logger.Info(`Server Running on port: ${port}`);
});
}

/**
* start Database first then the server
*/
public async startDB(): Promise<any> {
Logger.Info('Setting up database ...');
try {
await createConnection();
this.startServer();
Logger.Info('Database connected');
} catch (error) {
Logger.Warn(error);
return Promise.reject('Server Failed, Restart again...');
}
}
}

我读了这个question - 这就是我调用方法 startDB 的原因。

最佳答案

所以我想通了,解决方案很简单。虽然我无法解释原因。

这个 req.get('api/v1/users/') 应该是 /api/v1/users - 你需要一个前导 /.

关于node.js - Jest 测试通过但最后出现错误 : connect ECONNREFUSED 127. 0.0.1:80,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59188564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com