【问题标题】:No response when try test nodejs express4 route with chai-http尝试使用 chai-http 测试节点 js express 路由时没有响应
【发布时间】:2017-06-13 23:10:24
【问题描述】:

如果尝试运行应用程序并执行本机 (POstMan) 测试,我尝试对 nodejs express4 路由运行测试,但无法获得响应 - 像 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 毫秒的超时。对于异步测试和钩子,确保 “完成()”被调用;如果返回一个 Promise,请确保它解析。

【问题讨论】:

    标签: node.js typescript chai chai-http


    【解决方案1】:

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

    res.status(200)只设置http响应码为200,不发送 任何回应。你可以试试resp.sendStatus(200)

    【讨论】:

    • 我试试res.status(200).send({}),这行得通,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2012-08-06
    • 2023-03-08
    相关资源
    最近更新 更多