When doing model unit test in the nodejs 12.15/sequelize 5.21 app with jest 25.x, I notice there is some error message about the callback for the db.authenticate():
ā Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "DB connection has been established successfully.".
32 | db
33 | .authenticate()
> 34 | .then(() => {
| ^
35 | console.log('DB connection has been established successfully.');
36 | // The event will be called when a client is connected.
37 |
at CustomConsole.log (node_modules/@jest/console/build/CustomConsole.js:186:10)
at startup/db.js:34:11
at tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (node_modules/bluebird/js/release/async.js:86:9)
at Async.Object.<anonymous>.Async._drainQueues (node_modules/bluebird/js/release/async.js:102:5)
The code causing the error is for establishing the connection to database:
const Sql = require("sequelize");
const db = new Sql('my_db', 'postgres', `${process.env.DB_PASSWORD}`, {
host: 'localhost',
dialect: 'postgres',
port:5432,
} );
db.authenticate()
.then(() => {
console.log('DB connection has been established successfully.'); //<<<<===this callback caused error
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
The error occurs for every model.test.js file executed. The db connection is established at top level index.js and there is no way to do a async/await except the callback. Since this is not a real bug in the code, how to make the error disappear?