await does throw and bubble up, but not always
Take a look at below:
[code] console.log(‘XXX 1’) await serverInstance.httpServer.destroy(); console.log(‘XXX 2’) await serverInstance.dbConnection.close(); await db.dropDatabase(); await db.close(); console.log(‘XXX 3’) await serverInstance.close(); console.log(‘XXX 4’) [/code]
XXX 4 never prints. It does exit.
[code] console.log(‘XXX 1’) await serverInstance.httpServer.destroy(); console.log(‘XXX 2’) await serverInstance.close(); console.log(‘XXX 3’) await serverInstance.dbConnection.close(); await db.dropDatabase(); await db.close(); console.log(‘XXX 4’) [/code]
xxx3 and xxx4 never called, db connection never closed, process doesn’t exit.
How can this happen??
Context: it’s inside a Jest afterAll hook.
So surprise surprise, close is not defined on serverInstance. It err, promise rejected. It should throw. And it does. It’s not caught because it’s already in a finally block (or not, doesn’t matter, as long as it’s not inside a try block). It bubbles up. Somewhere, Jest will have to swallow it. Because if it’s not, it becomes a top level promise rejection in a plain script. It won’t. Jest swallows it silently.
With that, anything after serverInstance.close()
line is not executed, process never exit. It has nothing to do with http connection that doesn’t close, nothing to do with db connection or dropDatabase()
So try catch await calls, really.