Check mongoose connection state without creating new connection

后端 未结 2 1417
离开以前
离开以前 2021-01-30 12:43

I have some tests - namely Supertest - that load my Express app. This app creates a Mongoose connection. I would like to know how to check the status of that connection from wit

2条回答
  •  执笔经年
    2021-01-30 13:02

    Since the mongoose module exports a singleton object, you don't have to connect in your test.js to check the state of the connection:

    // test.js
    require('./app.js'); // which executes 'mongoose.connect()'
    
    var mongoose = require('mongoose');
    console.log(mongoose.connection.readyState);
    

    ready states being:

    • 0: disconnected
    • 1: connected
    • 2: connecting
    • 3: disconnecting

提交回复
热议问题