Jest Database Seed (using beforeAll, MongoDriver)

?
S
Bash

Snippet to add and remove data on MongoDB via beforeAll and afterAll hooks "test": "jest --forceExit --runInBand --coverage --watchAll",

1import { Types, MongooseDocument } from 'mongoose';
2import mongoose from 'mongoose';
3import MongoConnection from '../scripts/mongo-connection';
4const { ObjectId } = Types;
5let SlotModel;
6
7const slotPayload = {
8      createdOn: '2020-07-14T03:01:56.238+0000',
9      status: 'ON_FLIGHT',
10      confirmation: 'ccdba54f7c5772089887c321d9eb9695',
11    };
12};
13const seedTestData = async () => {
14  SlotModel = MongoConnection.db.model('Slot', new mongoose.Schema({}));
15  const destinationSlot = await SlotModel.collection.insert(slotPayload);
16  return;
17};
18
19const removeTestData = async () => {
20  const destinationSlot = await SlotModel.remove({ _id: slotPayload._id });
21  return;
22};
23
24export { seedTestData, removeTestData };
25

Created on 8/14/2020