0

I am trying to write some basic tests

Here is my server.js

import express from 'express';

export default class Server {
  constructor(options) {
    this.port = options.port;
    this.express = express();
  }

  initialize() {
    this.configureRoutes();
    this.configureMiddleware();
  }

  start() {
    this.express.listen(this.port, () => {
      console.log(`Server listening on port ${this.port}!`);
    })
  }

  configureMiddleware() {
    // TODO
  }

  configureRoutes() {
    this.express.get('/', (req, res) => {
      res.status(200).send('Hello World!');
    });
  }

};

I like the class approach because it lets me to inject different types of configurations.

Here is my index.js which I am not using in testing, but shows that the server.js is working

import dotenv from 'dotenv';

import Server from './server';

dotenv.config();

const port = process.env.PORT;

const options = {
  port
};

let server = new Server(options);
server.initialize();
server.start();

export default server;

and finally my server_spec.js

import dotenv from 'dotenv';
import chai from 'chai';
import chaiHttp from 'chai-http';

import Server from '../../src/server';

dotenv.config();

chai.use(chaiHttp);


describe('Red Pill Server', () => {
  let server;

  before('setup server', done => {
    const port = process.env.PORT;
    console.log(`port ${port}`);
    const options = {
      port
    };
    server = new Server(options);
    server.initialize();
    server.start();
    done();
  });

  it('should get a response from server', done => {
    chai.request(server).get('/').end( (err, res) => {
      if (err) {
        console.log(`err ->`, err);
      } else {
        expect(res).to.have.status(200);
      }
      done();
    })
  });

});

I am getting TypeError: app.address is not a function and I looked to this stackoverflow question that it is related but I cannot figure out the problem. Since I am exporting the class and initializing in the tests.

agusgambina
  • 6,229
  • 14
  • 54
  • 94

2 Answers2

0

Try adding this to your app.js file:

module.exports = server
Ravi Sharma
  • 507
  • 6
  • 21
0

Finally with some small changes it started working.

The first step is to realize that chai is expecting the "listening server", so in the server.js I made this small change

  start() {
    return this.app.listen(this.port, () => {
      console.log(`Server listening on port ${this.port}!`);
    })
  }

After this change I changed the way I described the test

describe('Red Pill Server', () => {
  let app;

  beforeEach(done => {
    const port = process.env.PORT;
    const host = process.env.HOST;
    console.log(`port ${port}`);
    const options = {
      port,
      host
    };
    let server = new Server(options);
    server.initialize();
    app = server.start();
    done();
  });

  afterEach(done => {
    app.close();
    done();
  });

  it('should get a response from server', done => {
    chai.request(app).get('/').end( (err, res) => {
      expect(res).to.have.status(200);
      expect(res.body).to.eql({ message: 'Hello World!' });
      done();
    });
  });

});

It is like I am creating/destroying the server in each test, that in the application is created in the index.js

agusgambina
  • 6,229
  • 14
  • 54
  • 94