I am trying to use with with mocha tests and I am finding that there's no way to close the express server after my tests complete. This causes issues with other tests that try to bind to the same port and fail because jsonServer is still running.
I solved my problem by returning app.listen to server so I can close
var expect = require('expect.js'),
jsonServer = require('json-server'),
path = require('path'),
server = {};
describe('sports service', () => {
before(() => {
// Starting the server
var app = jsonServer.create();
const router = jsonServer.router(path.join(__dirname, '../../../data/sports.db.json'));
const middlewares = jsonServer.defaults();
const version = 'v3'
const port = 8080;
var sportId = 'australian-football';
var leagueId = 'afl';
app.use(middlewares);
app.use(jsonServer.bodyParser);
app.use(`/${version}/sports/${sportId}/${leagueId}`, router);
server = app.listen(port, () => {
// console.log('JSON Server is running');
});
});
after(() => {
server.close();
})
Most helpful comment
I solved my problem by returning
app.listentoserverso I can close