All files / Backend/tests db.js

100% Statements 21/21
50% Branches 2/4
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  4x 4x   4x                   4x 4x   4x 4x   4x                   4x 4x           4x 4x   4x 4x 4x           4x 11x 11x   11x 33x 33x      
 
const mongoose = require('mongoose');
const { MongoMemoryServer } = require('mongodb-memory-server');
 
let mongod = undefined;
 
 
 //Connect to the in-memory database.
//  mongod = new MongoMemoryServer({
//     instance: {
//       port: 8080, // by default choose any free port
//     }
// });
 
module.exports.connect = async () => {
    await mongoose.disconnect();
 
    mongod = await MongoMemoryServer.create();
    const uri = mongod.getUri();
 
    const mongooseOpts = {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
        autoReconnect: true,
        reconnectTries: Number.MAX_VALUE,
        reconnectInterval: 1000,
   
    };
 
    await mongoose.connect(uri, mongooseOpts);
    console.log("connected (tests)")
};
 
 
// Drop database, close the connection and stop mongod.
 
module.exports.closeDatabase = async () => {
    Eif (mongod) {
        // await mongoose.connection.dropDatabase();
        await mongoose.connection.close();
        await mongod.stop();
        console.log('close data base')
    }
};
 
// Remove all the data for all db collections.
 
module.exports.clearDatabase = async () => {
    Eif (mongod) {
        const collections = mongoose.connection.collections;
 
        for (const key in collections) {
            const collection = collections[key];
            await collection.deleteMany();
        }
    }
};