I'm having some difficulty figuring out how to properly test this bit of code:
const chai = require('chai')
const chaiHttp = require('chai-http')
const restify = require('restify')
const errs = require('restify-errors')
const expect = chai.expect
chai.use(chaiHttp)
const willerror = true
const server = restify.createServer()
server.get('/', function (req, res, next) {
if (!willerror) res.send()
next(willerror ? new errs.InternalServerError() : null)
})
describe('Server test', function () {
before(async function () {
const c = {config: {dev: {driver: 'sqlite3', filename: ':memory:'}}}
const dbm = require('db-migrate').getInstance(true, c)
dbm.silence(true)
await dbm.reset().then(() => dbm.up('all', 'all'))
})
after(function (done) {
server.close(done)
})
it('posts', function (done) {
const client = chai.request(server)
client.get('/')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
done()
})
})
})
(Note: it requires at least one migration, but it can be empty: db-migrate create one
should suffice).
The output I get:
Removing the contents of the before()
call (leaving just before(async function(){})
) gives what what I'd expect:
Why aren't these tests finishing?
Turns out db-migrate registers an uncaughtException
handler.
To defeat it, add throwUncatched: true
to the configuration object passed to db-migrate
. In my case:
const c = {throwUncatched: true, config: {dev: {driver: 'sqlite3', filename: ':memory:'}}}