@@ -2,29 +2,13 @@ const mongoose = require('mongoose');
22const mongo = require ( '../chronos_npm_package/controllers/mongo' ) ;
33const ServicesModel = require ( '../chronos_npm_package/models/ServicesModel' ) ;
44const CommunicationModel = require ( '../chronos_npm_package/models/CommunicationModel' ) ;
5- const { connectDB, dropDB, dropCollections } = require ( './mockdbsetup' ) ;
5+ const { connectDB, dropDB, dropCollections, uri } = require ( './mockdbsetup' ) ;
66const alert = require ( '../chronos_npm_package/controllers/alert' ) ;
7- const ContainerInfoFunc = require ( '../chronos_npm_package/models/ContainerInfo' ) ;
87const dockerHelper = require ( '../chronos_npm_package/controllers/dockerHelper' ) ;
98
10- require ( 'dotenv' ) . config ( ) ;
11-
12- const db = process . env . DB_URI ;
13-
14- beforeAll ( async ( ) => {
15- await connectDB ( ) ;
16- } ) ;
17-
18- afterAll ( async ( ) => {
19- await dropDB ( ) ;
20- } ) ;
21-
229jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
23-
2410jest . mock ( '../chronos_npm_package/controllers/alert' ) ;
25-
2611jest . useFakeTimers ( ) ;
27-
2812jest . spyOn ( global , 'setInterval' ) ;
2913
3014jest . mock ( '../chronos_npm_package/controllers/healthHelpers' , ( ) => {
@@ -53,31 +37,22 @@ jest.mock('../chronos_npm_package/controllers/mongo', () => ({
5337const HealthModel = {
5438 insertMany : jest . fn ( ( ) => Promise . resolve ( ) ) ,
5539} ;
40+ const HealthModelFunc = jest . fn ( ( ) => HealthModel ) ;
5641
5742jest . mock ( '../chronos_npm_package/controllers/dockerHelper' , ( ) => ( {
5843 ...jest . requireActual ( '../chronos_npm_package/controllers/dockerHelper' ) ,
5944 getDockerContainer : jest . fn ( ) ,
6045 readDockerContainer : jest . fn ( ) ,
6146} ) ) ;
6247
63- // jest.mock('../../chronos_npm_package/models/ContainerInfo', () => {
64- // const mockContainerInfoInstance = {
65- // create: jest.fn(),
66- // };
67- // return jest.fn(() => mockContainerInfoInstance);
68- // });
69-
70- const HealthModelFunc = jest . fn ( ( ) => HealthModel ) ;
71-
7248describe ( 'mongo.connect' , ( ) => {
7349 beforeEach ( ( ) => {
7450 jest . clearAllMocks ( ) ;
7551 } ) ;
7652
7753 test ( 'should connect to MongoDB database' , async ( ) => {
78- await mongo . connect ( { database : { URI : db } } ) ;
54+ await mongo . connect ( { database : { URI : uri } } ) ;
7955
80- //expect(mongoose.connect).toHaveBeenCalledWith(db);
8156 expect ( console . log ) . toHaveBeenCalledWith (
8257 expect . stringContaining ( 'MongoDB database connected at' )
8358 ) ;
@@ -95,12 +70,14 @@ describe('mongo.connect', () => {
9570} ) ;
9671
9772describe ( 'mongo.services' , ( ) => {
98- beforeEach ( ( ) => {
73+ beforeEach ( async ( ) => {
9974 jest . clearAllMocks ( ) ;
75+ await connectDB ( ) ;
10076 } ) ;
10177
10278 afterEach ( async ( ) => {
10379 await dropCollections ( ) ;
80+ await dropDB ( ) ;
10481 } ) ;
10582
10683 test ( 'should create a new document' , async ( ) => {
@@ -111,12 +88,14 @@ describe('mongo.services', () => {
11188} ) ;
11289
11390describe ( 'mongo.communications' , ( ) => {
114- beforeEach ( ( ) => {
91+ beforeEach ( async ( ) => {
11592 jest . clearAllMocks ( ) ;
93+ await connectDB ( ) ;
11694 } ) ;
11795
11896 afterEach ( async ( ) => {
11997 await dropCollections ( ) ;
98+ await dropDB ( ) ;
12099 } ) ;
121100
122101 test ( 'should record request cycle and save communication to the database' , async ( ) => {
@@ -134,7 +113,7 @@ describe('mongo.communications', () => {
134113 const middleware = mongo . communications ( { microservice : 'test3' , slack : null , email : null } ) ;
135114 await middleware ( req , res , ( ) => { } ) ;
136115 const savedCommunication = await CommunicationModel . findOne ( { microservice : 'test3' } ) ;
137- expect ( savedCommunication ) . toBeDefined ( ) ; // The document should be defined if it exists
116+ expect ( savedCommunication ) . toBeDefined ( ) ;
138117 } ) ;
139118
140119 test ( 'should send an alert' , async ( ) => {
@@ -163,13 +142,16 @@ describe('mongo.communications', () => {
163142} ) ;
164143
165144describe ( 'mongo.health' , ( ) => {
166- beforeEach ( ( ) => {
145+ beforeEach ( async ( ) => {
167146 jest . clearAllMocks ( ) ;
168147 jest . useFakeTimers ( ) ;
148+ await connectDB ( ) ;
169149 } ) ;
170150
171- afterEach ( ( ) => {
151+ afterEach ( async ( ) => {
172152 jest . clearAllTimers ( ) ;
153+ await dropCollections ( ) ;
154+ await dropDB ( ) ;
173155 } ) ;
174156
175157 test ( 'should collect data after the set interval' , async ( ) => {
@@ -206,11 +188,12 @@ describe('mongo.docker', () => {
206188 beforeEach ( async ( ) => {
207189 jest . clearAllMocks ( ) ;
208190 jest . useFakeTimers ( ) ;
191+ await connectDB ( ) ;
209192 } ) ;
210193
211194 afterEach ( async ( ) => {
212195 await dropCollections ( ) ;
213- jest . clearAllTimers ( ) ;
196+ await dropDB ( ) ;
214197 } ) ;
215198
216199 test ( 'should collect docker container information' , async ( ) => {
@@ -241,6 +224,5 @@ describe('mongo.docker', () => {
241224 expect ( dockerHelper . getDockerContainer ) . toHaveBeenCalledWith ( microservice ) ;
242225 jest . advanceTimersByTime ( 1000 ) ;
243226 expect ( dockerHelper . readDockerContainer ) . toHaveBeenCalledWith ( mockContainerData ) ;
244- //expect(mockContainerInfoInstance.create).toHaveBeenCalledWith(mockReadDockerContainerData);
245227 } ) ;
246228} ) ;
0 commit comments