vendredi 4 décembre 2020

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:

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?

Aucun commentaire:

Enregistrer un commentaire