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.