尝试使用chai-http测试nodejs express4路由时无响应 [英] No response when try test nodejs express4 route with chai-http

查看:79
本文介绍了尝试使用chai-http测试nodejs express4路由时无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果尝试运行应用程序并执行本机(POstMan)测试,则我尝试对nodejs express4路由进行测试,但无法获得响应-就像一个沙姆(sharm).

I try run test for nodejs express4 routes and can't get response, if try run app and perform native (POstMan) test - work like a sharm).

APP.ts

import express = require('express');
import expressValidator = require('express-validator');
import bodyParser = require('body-parser');
let app = express();

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(expressValidator())

app.get("/", (req, res) => res.status(200).send({message: "Hello"}));

app.route("/book")
    .get((req,res)=>{console.log(req.path);return res.status(200)})
    .post((req,res)=>{return res.status(

//Run
app.listen(3000, function() {
  console.log('Listening on port '+ 3000)
})

export = app;

test.ts

import chai = require('chai');
import chaiHttp = require('chai-http');
import mocha = require('mocha');

let server = require('../dist/app');
let expect = chai.expect;

chai.use(chaiHttp);

describe('/GET', () => {
      it('it should not GET a book without pages field', (done) => {
                        chai.request(server)
                        .get('/book')
                        .end(function(err, res) {
                            expect(res).to.have.status(200);
                            done();                               // <= Call done to signal callback end
                        });
            });
  });

结果我得到了错误

错误:超时超过2000毫秒.对于异步测试和挂钩,请确保称为"done()";如果返回了Promise,请确保它可以解决.

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

推荐答案

/book 不会将任何响应发送回客户端.这就是您的测试用例超时的原因.

/book doesn't send any response back to client. That's why your testcase is timing out.

res.status(200)仅将http响应代码设置为200,但不发送任何回应.您可以尝试 resp.sendStatus(200).

res.status(200) only set the http response code to 200, but doesn't send any response. You can try resp.sendStatus(200).

这篇关于尝试使用chai-http测试nodejs express4路由时无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆