简体   繁体   English

TypeError: app.address is not a function using chai-http

[英]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.

相关问题 使用Mocha&Chai进行节点测试-&gt;错误。TypeError:app.address不是函数 - Node Testing with Mocha & Chai -> Error.. TypeError: app.address is not a function Jest - TypeError: app.address 不是路由器的 function - Jest - TypeError: app.address is not a function for router Express javascript 单元测试“TypeError:app.address 不是函数” - Express javascript unit test 'TypeError: app.address is not a function' “TypeError: app.address is not a function” 尽管导出了服务器 - “TypeError: app.address is not a function” despite exporting the server TypeScript + Mocha + Express-TypeError:app.address不是函数 - TypeScript + Mocha + Express - TypeError: app.address is not a function Mocha API 测试:得到“TypeError:app.address 不是函数” - Mocha API Testing: getting 'TypeError: app.address is not a function' 使用chai-http与mocha-chai进行测试期间的UnhandledPromiseRejectionWarning - UnhandledPromiseRejectionWarning during testing with mocha-chai using chai-http 如何为chai-http导出节点快递app - How to export node express app for chai-http 尝试在我的 nodejs api 中运行 Jest/Supertest 但不断收到 TypeError:“app.address 不是函数” - Attempting to run Jest/Supertest in my nodejs api but keep getting a TypeError : "app.address is not a function" 挂钩之前的Mocha不适用于chai-http - Mocha before hook is not working with chai-http
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM