尝试使用 chai-http 测试节点 js express 4 路由时无响应
No response when try test nodejs express4 route with chai-http
我尝试 运行 测试 nodejs express4 路由但无法得到响应,如果尝试 运行 应用程序并执行本机 (POstMan) 测试 - 工作得很好。
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
});
});
});
结果我收到错误
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure
"done()" is called; if returning a Promise, ensure it resolves.
/book
不会将任何响应发送回客户端。这就是您的测试用例超时的原因。
res.status(200)
只设置http响应码为200,不发送
任何回应。你可以试试 resp.sendStatus(200)
.
我尝试 运行 测试 nodejs express4 路由但无法得到响应,如果尝试 运行 应用程序并执行本机 (POstMan) 测试 - 工作得很好。
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
});
});
});
结果我收到错误
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
/book
不会将任何响应发送回客户端。这就是您的测试用例超时的原因。
res.status(200)
只设置http响应码为200,不发送
任何回应。你可以试试 resp.sendStatus(200)
.