Ошибка: подключите ECONNREFUSED 127.0.0.1:3100 - PullRequest
0 голосов
/ 25 сентября 2018

Я новичок в узле. Я получаю сообщение об ошибке Ошибка: подключиться ECONNREFUSED 127.0.0.1:3100, когда я запускаю тест npm.

Это мой тестовый файл.

Test.ts

import * as chai from 'chai';
let chaiHttp = require('chai-http');
import * as assert from 'assertthat';
import * as request from "superagent";
chai.use(chaiHttp);
const expect = chai.expect;
describe('Checking whether the response return status 200', function() {
it('Status OK', function(done) {
return chai.request('https://localhost:3100') 
.get('/hello')
.end(function(err, res){
    if(err){
        done(err);
    }
    else{
        expect(res.body.message).to.equal('hello world');
        done();
    }
});
});
});

Это файл моего приложения

app.ts

import * as express from 'express';
import {Request,Response} from 'express';
const app: express.Express = express();

app.get('/hello',(req:Request,res:Response)=>{
    res.json({
        message:"hello world"
    });
});
app.listen(3100,() =>{
    console.log("server listening");
});
export default app;

1 Ответ

0 голосов
/ 25 сентября 2018

Ваш сервер не работает при попытке проверить GET / hello маршрут, поэтому он не может подключиться.

Смотрите здесь пример о том, как следуетпротестируйте серверные маршруты вашего API.

В вашем файле test.ts вы должны импортировать свой сервер и убедиться, что он прослушивает перед тестами и закрывает после испытаний.

import server from 'app.ts';
import * as chai from 'chai';
import * as assert from 'assertthat';
import * as request from 'superagent';

const chaiHttp = require('chai-http');
const expect = chai.expect;

chai.use(chaiHttp);

describe('Checking whether the response return status 200', function() {
    it('Status OK', async function(done) {
        const { res, err } = await chai.request(server).get('/hello');
        expect(res.body.message).to.equal('hello world');
    });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...