0

I'm trying to create a micro-api with Fastify and now I'm testing the app but I get this error:

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?

Ming Ye
  • 33
  • 3

2 Answers2

1

chai.request(app) doesn't accept a fastify instance as input as documented:

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

You should start the fastify server and give it to 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:

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.

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
0
// 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();
      });
    });
  });
});