I'm trying to get a TDD workflow going with koa2/mocha/chai/chai-http but my problem is that when I run the tests the koa2 server keeps running after the tests are finished. So that I have to Ctrl+C (kill) it every time.
Can anyone tell me how to setup a TDD workflow where the server gets stopped after all tests are run?
Also, I'd like to watch the test files for changes and re-run the tests as soon as changes are detected... can anyone help with that? can't find anything on the net -.-
What I currently have (simplified):
package.json:
"scripts": {
"watch-server": "nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/server.ts",
"test": "./node_modules/mocha/bin/mocha --compilers ts:ts-node/register test/**/*.ts"
},
server.ts:
app.use(routes_v1.routes());
export const server = app.listen(3000, () => {
console.log('Server running on port 3000');
});
test:
process.env.NODE_ENV = 'test';
import * as chai from 'chai';
const chaiHttp = require('chai-http');
const should = chai.should();
chai.use(chaiHttp);
import { server } from '../../../src/server';
describe('routes : login / register', () => {
describe('POST /sign_in', () => {
it('should return unauthorized for invalid user', (done) => {
chai.request(server)
.post('/sign_in')
.send({email: "test@test.de", password: "somePassword"})
.end((err, res) => {
res.status.should.eql(401);
should.exist(err);
done();
});
});
it('should return authorized for valid user', (done) => {
chai.request(server)
.post('/sign_in')
.send({email: 'authorized@test.de', password: "authorizedPassword"})
.end((err, res) => {
res.status.should.eql(200);
should.exist(res.body.token);
done();
});
});
});
Thank you.