@@ -5,29 +5,13 @@ const mongoose = require('mongoose');
55const mongo = require ( '../chronos_npm_package/controllers/mongo' ) ;
66const ServicesModel = require ( '../chronos_npm_package/models/ServicesModel' ) ;
77const CommunicationModel = require ( '../chronos_npm_package/models/CommunicationModel' ) ;
8- const { connectDB, dropDB, dropCollections } = require ( './mockdbsetup' ) ;
8+ const { connectDB, dropDB, dropCollections, uri } = require ( './mockdbsetup' ) ;
99const alert = require ( '../chronos_npm_package/controllers/alert' ) ;
10- const ContainerInfoFunc = require ( '../chronos_npm_package/models/ContainerInfo' ) ;
1110const dockerHelper = require ( '../chronos_npm_package/controllers/dockerHelper' ) ;
1211
13- require ( 'dotenv' ) . config ( ) ;
14-
15- const db = 'mongodb+srv://Ok:Ok@cluster0.axo7iyw.mongodb.net/?retryWrites=true&w=majority' ;
16-
17- beforeAll ( async ( ) => {
18- await connectDB ( ) ;
19- } ) ;
20-
21- afterAll ( async ( ) => {
22- await dropDB ( ) ;
23- } ) ;
24-
2512jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
26-
2713jest . mock ( '../chronos_npm_package/controllers/alert' ) ;
28-
2914jest . useFakeTimers ( ) ;
30-
3115jest . spyOn ( global , 'setInterval' ) ;
3216
3317jest . mock ( '../chronos_npm_package/controllers/healthHelpers' , ( ) => {
@@ -56,34 +40,25 @@ jest.mock('../chronos_npm_package/controllers/mongo', () => ({
5640const HealthModel = {
5741 insertMany : jest . fn ( ( ) => Promise . resolve ( ) ) ,
5842} ;
43+ const HealthModelFunc = jest . fn ( ( ) => HealthModel ) ;
5944
6045jest . mock ( '../chronos_npm_package/controllers/dockerHelper' , ( ) => ( {
6146 ...jest . requireActual ( '../chronos_npm_package/controllers/dockerHelper' ) ,
6247 getDockerContainer : jest . fn ( ) ,
6348 readDockerContainer : jest . fn ( ) ,
6449} ) ) ;
6550
66- // jest.mock('../../chronos_npm_package/models/ContainerInfo', () => {
67- // const mockContainerInfoInstance = {
68- // create: jest.fn(),
69- // };
70- // return jest.fn(() => mockContainerInfoInstance);
71- // });
72-
73- const HealthModelFunc = jest . fn ( ( ) => HealthModel ) ;
74-
7551describe ( 'mongo.connect' , ( ) => {
7652 beforeEach ( ( ) => {
7753 jest . clearAllMocks ( ) ;
7854 } ) ;
7955
8056 test ( 'should connect to MongoDB database' , async ( ) => {
81- await mongo . connect ( { database : { URI : db } } ) ;
57+ await mongo . connect ( { database : { URI : uri } } ) ;
8258
83- expect ( mongoose . connect ) . toHaveBeenCalledWith ( db ) ;
84- // expect(console.log).toHaveBeenCalledWith(
85- // expect.stringContaining('MongoDB database connected at')
86- // );
59+ expect ( console . log ) . toHaveBeenCalledWith (
60+ expect . stringContaining ( 'MongoDB database connected at' )
61+ ) ;
8762 } ) ;
8863
8964 test ( 'should handle connection errors' , async ( ) => {
@@ -98,12 +73,14 @@ describe('mongo.connect', () => {
9873} ) ;
9974
10075describe ( 'mongo.services' , ( ) => {
101- beforeEach ( ( ) => {
76+ beforeEach ( async ( ) => {
10277 jest . clearAllMocks ( ) ;
78+ await connectDB ( ) ;
10379 } ) ;
10480
10581 afterEach ( async ( ) => {
10682 await dropCollections ( ) ;
83+ await dropDB ( ) ;
10784 } ) ;
10885
10986 test ( 'should create a new document' , async ( ) => {
@@ -114,12 +91,14 @@ describe('mongo.services', () => {
11491} ) ;
11592
11693describe ( 'mongo.communications' , ( ) => {
117- beforeEach ( ( ) => {
94+ beforeEach ( async ( ) => {
11895 jest . clearAllMocks ( ) ;
96+ await connectDB ( ) ;
11997 } ) ;
12098
12199 afterEach ( async ( ) => {
122100 await dropCollections ( ) ;
101+ await dropDB ( ) ;
123102 } ) ;
124103
125104 test ( 'should record request cycle and save communication to the database' , async ( ) => {
@@ -137,7 +116,7 @@ describe('mongo.communications', () => {
137116 const middleware = mongo . communications ( { microservice : 'test3' , slack : null , email : null } ) ;
138117 await middleware ( req , res , ( ) => { } ) ;
139118 const savedCommunication = await CommunicationModel . findOne ( { microservice : 'test3' } ) ;
140- expect ( savedCommunication ) . toBeDefined ( ) ; // The document should be defined if it exists
119+ expect ( savedCommunication ) . toBeDefined ( ) ;
141120 } ) ;
142121
143122 test ( 'should send an alert' , async ( ) => {
@@ -166,13 +145,16 @@ describe('mongo.communications', () => {
166145} ) ;
167146
168147describe ( 'mongo.health' , ( ) => {
169- beforeEach ( ( ) => {
148+ beforeEach ( async ( ) => {
170149 jest . clearAllMocks ( ) ;
171150 jest . useFakeTimers ( ) ;
151+ await connectDB ( ) ;
172152 } ) ;
173153
174- afterEach ( ( ) => {
154+ afterEach ( async ( ) => {
175155 jest . clearAllTimers ( ) ;
156+ await dropCollections ( ) ;
157+ await dropDB ( ) ;
176158 } ) ;
177159
178160 test ( 'should collect data after the set interval' , async ( ) => {
@@ -209,11 +191,12 @@ describe('mongo.docker', () => {
209191 beforeEach ( async ( ) => {
210192 jest . clearAllMocks ( ) ;
211193 jest . useFakeTimers ( ) ;
194+ await connectDB ( ) ;
212195 } ) ;
213196
214197 afterEach ( async ( ) => {
215198 await dropCollections ( ) ;
216- jest . clearAllTimers ( ) ;
199+ await dropDB ( ) ;
217200 } ) ;
218201
219202 test ( 'should collect docker container information' , async ( ) => {
@@ -244,6 +227,5 @@ describe('mongo.docker', () => {
244227 expect ( dockerHelper . getDockerContainer ) . toHaveBeenCalledWith ( microservice ) ;
245228 jest . advanceTimersByTime ( 1000 ) ;
246229 expect ( dockerHelper . readDockerContainer ) . toHaveBeenCalledWith ( mockContainerData ) ;
247- //expect(mockContainerInfoInstance.create).toHaveBeenCalledWith(mockReadDockerContainerData);
248230 } ) ;
249231} ) ;
0 commit comments