0.0.2 • Published 3 years ago
mongo-easymemorydb v0.0.2
mongo-easymemorydb
Library that allows creating a mongodb database in memory in memory, the connection to moongose is automatic, so the use of moongose models are ready to use.
How use this
- just call the library and call the moongose model you want to use in the test
const { expect, test } = require('@jest/globals');
const mongodbInMemory = require('../createMongoDb');
const { userModel } = require('./userModel');
test('test: check creation,operation and close of BD in memory', async () => {
await mongodbInMemory.connect();
const newValues = {
sub: '012345678900123456789',
email: 'elbrick.salazar@gmail.com',
name: 'elbrick salazar',
roles: ['role_user'],
balance: 0.0,
address: 'Av Bolivar'
};
const newRegister = new userModel(newValues);
const dataToReturn = (await newRegister.save()).toJSON();
expect(dataToReturn['_id'], 'debe existir el campo _id').not.toBeUndefined();
//
mongodbInMemory.clearDatabase();
const returnQuery = await userModel.findOne(dataToReturn._id).lean();
expect(returnQuery, 'No debe existir el registro en BD').toBeNull();
//
await mongodbInMemory.closeDatabase();
});