In order to automate testing of my Express.js, I want to make sure that the tests have finished running when each test decides the results.
Consider this simplified model of that situation:
main.js
#!/usr/bin/env node
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('hello world')
})
const server = app.listen(3000, function () {
// I would run tests here, which is the 'listen' event.
console.log('Server started')
// After the tests, I will close the server from here.
this.close()
})
// I want to make sure that this runs only after the asserts were done,
// otherwise the test would miss possible failures.
console.log('After listen')
package.json
{
"name": "tmp",
"version": "1.0.0",
"dependencies": {
"express": "4.17.1"
}
}
Running main
produces:
After listen
Server started
because the server must be asynchronously starting to listen to connections, so After listen
runs first. But I want instead to have:
Server started
After listen
I know about the 'close'
event which would allow me to write:
#!/usr/bin/env node
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('hello world')
})
const server = app.listen(3000, function () {
// I would run tests here, which is the 'listen' event.
console.log('Server started')
// After the tests, I will close the server from here.
this.close()
})
server.on('close', () => {
console.log('After listen')
})
but then this just shifts the problem further out to the test infrastructure, which has to make sure that it is able to wait for the After listen
to happen. Yes, in Mocha this is possible with done()
, but it would be better to have something simpler more test-system agnostic.
Tested on Node.js 14.15.0.
Related: