[英]TypeError: app.address is not a function using chai-http
I'm trying to create a micro-api with Fastify and now I'm testing the app but I get this error:我正在尝试使用 Fastify 创建一个微 API,现在我正在测试该应用程序,但我收到此错误:
Testing /allstyles
Should return all style names:
TypeError: app.address is not a function
at serverAddress (node_modules/chai-http/lib/request.js:282:18)
at new Test (node_modules/chai-http/lib/request.js:271:53)
at Object.obj.<computed> [as get] (node_modules/chai-http/lib/request.js:239:14)
at Context.<anonymous> (test/routes-chai.js:12:8)
at processImmediate (internal/timers.js:461:21)
My app file is this one:我的应用程序文件是这个:
const fastify = require('fastify');
var app = fastify({
logger:{level:'error',prettyPrint:true}
});
app.get('/',(req,res)=>{
console.log('Hello world');
res.code(200);
});
module.exports = app;
and my test file is:我的测试文件是:
var expect = require('chai').expect;
var app = require('../app/main.js');
var chaiHttp = require('chai-http');
var chai = require('chai');
chai.use(chaiHttp);
describe('Testing routes',()=>{
describe('Testing /allstyles',()=>{
it('Should return all style names',(done)=>{
chai.request(app)
.get('/')
.end((err,res)=>{
expect(res).to.have.status(200);
done();
});
});
});
});
I have tried with:我尝试过:
module.exports = app.listen(3000);
and和
module.exports = {app}
but it always give me back some error like this one or other that says:但它总是给我一些类似这样的错误:
TypeError: Cannot read property 'address' of undefined
Does someone know what am I doing wrong?有人知道我在做什么错吗?
chai.request(app)
doesn't accept a fastify instance as input as documented: chai.request(app)
不接受 fastify 实例作为记录的输入:
You may use a function (such as an express or connect app) or a node.js http(s) server as the foundation for your request您可以使用 function(例如 express 或 connect 应用程序)或 node.js http(s) 服务器作为请求的基础
You should start the fastify server and give it to chai:你应该启动 fastify 服务器并将其交给 chai:
var expect = require('chai').expect;
var app = require('./index.js');
var chaiHttp = require('chai-http');
var chai = require('chai');
chai.use(chaiHttp);
app.listen(8080)
.then(server => {
chai.request(server)
.get('/')
.end((err, res) => {
expect(res).to.have.status(200);
app.close()
});
})
This will works as expected.这将按预期工作。
NB: your HTTP handler doesn't call reply.send
, so the request will timeout, you need to fix it too:注意:您的 HTTP 处理程序未调用reply.send
,因此请求将超时,您也需要修复它:
app.get('/', (req, res) => {
console.log('Hello world');
res.code(200);
res.send('done')
});
As a side note, I would suggest trying the fastify.inject
feature that would avoid to start the server listening and it will speed up a lot your test and you will not have trouble with port already in use.作为旁注,我建议尝试使用fastify.inject
功能,该功能可以避免启动服务器侦听,它会大大加快您的测试速度,并且您不会遇到已经在使用的端口的问题。
// you must declare the app variable this way
var expect = require('chai').expect;
var app = require('../app/main.js').app;
var chaiHttp = require('chai-http');
var chai = require('chai');
chai.use(chaiHttp);
describe('Testing routes',()=>{
describe('Testing /allstyles',()=>{
it('Should return all style names',(done)=>{
chai.request(app)
.get('/')
.end((err,res)=>{
expect(res).to.have.status(200);
done();
});
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.