test: add mock db test
This commit is contained in:
45
test/helper/db-mock.helper.ts
Normal file
45
test/helper/db-mock.helper.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server'
|
||||
import mongoose from 'mongoose'
|
||||
|
||||
let mongod: MongoMemoryServer
|
||||
|
||||
/**
|
||||
|
||||
* Connect to mock memory db.
|
||||
*/
|
||||
const connect = async () => {
|
||||
mongod = await MongoMemoryServer.create()
|
||||
const uri = mongod.getUri()
|
||||
|
||||
await mongoose.connect(uri, {
|
||||
autoIndex: true,
|
||||
maxPoolSize: 10,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Close db connection
|
||||
*/
|
||||
const closeDatabase = async () => {
|
||||
await mongoose.connection.dropDatabase()
|
||||
await mongoose.connection.close()
|
||||
await mongod.stop()
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete db collections
|
||||
*/
|
||||
const clearDatabase = async () => {
|
||||
const collections = mongoose.connection.collections
|
||||
|
||||
for (const key in collections) {
|
||||
const collection = collections[key]
|
||||
await collection.deleteMany({})
|
||||
}
|
||||
}
|
||||
|
||||
export const dbHelper = {
|
||||
connect,
|
||||
close: closeDatabase,
|
||||
clear: clearDatabase,
|
||||
}
|
||||
Reference in New Issue
Block a user