I seem to be stuck, and I need a little bit of a hint to get me going.
I ended up stripping out all the socket.io code from server.js to ensure that my tests for express server are passing successfully first, which I'm unable to do. I'm not sure what is causing it to think that the server is not running despite that GET test has passed. My guess is that maybe server isn't ready before after() section gets ran.
I get this result:
- Express server is listening on port 80
- ✓ Should respond to / (154ms)
- Express has failed to disconnect Error [ERR_SERVER_NOT_RUNNING]: Server is not running.
- 1) "after all" hook
- 1 passing (2s)
- 1 failing
- Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
server.js
const express = require('express');
const socketio = require('socket.io');
const app = express();
app.use(express.static('public'));
app.get('/', (req, res) => {
res.status(200).sendFile(`${__dirname}/public/index.html`);
});
function startServer() {
const server = app.listen(PORT = 80, () => {
console.log(`Express server is listening on port ${PORT}`)
}).on('error', err => {
console.error('Express has failed to connect', err);
});
return server;
};
function stopServer(server) {
server.close(err => {
if (err) {
console.error('Express has failed to disconnect', err);
} else {
console.log('Express server has shut down');
}
});
}
module.exports = { app, startServer, stopServer };
server.test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const { app, startServer, stopServer } = require('../server');
const expect = chai.expect;
chai.use(chaiHttp);
describe('Integration tests for server.js', function() {
let server;
before(function() {
server = startServer();
});
after(function(done) {
return stopServer(server);
});
describe('GET', function() {
it('Should respond to /', async () => {
try {
const res = await chai.request(server).get('/');
expect(res).to.have.status(200);
} catch (err) {
console.error('Get / has failed');
}
})
})
});
Aucun commentaire:
Enregistrer un commentaire