@@ -5,29 +5,13 @@ const mongoose = require('mongoose');
5
5
const mongo = require ( '../chronos_npm_package/controllers/mongo' ) ;
6
6
const ServicesModel = require ( '../chronos_npm_package/models/ServicesModel' ) ;
7
7
const CommunicationModel = require ( '../chronos_npm_package/models/CommunicationModel' ) ;
8
- const { connectDB, dropDB, dropCollections } = require ( './mockdbsetup' ) ;
8
+ const { connectDB, dropDB, dropCollections, uri } = require ( './mockdbsetup' ) ;
9
9
const alert = require ( '../chronos_npm_package/controllers/alert' ) ;
10
- const ContainerInfoFunc = require ( '../chronos_npm_package/models/ContainerInfo' ) ;
11
10
const dockerHelper = require ( '../chronos_npm_package/controllers/dockerHelper' ) ;
12
11
13
- require ( 'dotenv' ) . config ( ) ;
14
-
15
- const db = 'mongodb+srv://Ok:[email protected] /?retryWrites=true&w=majority' ;
16
-
17
- beforeAll ( async ( ) => {
18
- await connectDB ( ) ;
19
- } ) ;
20
-
21
- afterAll ( async ( ) => {
22
- await dropDB ( ) ;
23
- } ) ;
24
-
25
12
jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
26
-
27
13
jest . mock ( '../chronos_npm_package/controllers/alert' ) ;
28
-
29
14
jest . useFakeTimers ( ) ;
30
-
31
15
jest . spyOn ( global , 'setInterval' ) ;
32
16
33
17
jest . mock ( '../chronos_npm_package/controllers/healthHelpers' , ( ) => {
@@ -56,34 +40,25 @@ jest.mock('../chronos_npm_package/controllers/mongo', () => ({
56
40
const HealthModel = {
57
41
insertMany : jest . fn ( ( ) => Promise . resolve ( ) ) ,
58
42
} ;
43
+ const HealthModelFunc = jest . fn ( ( ) => HealthModel ) ;
59
44
60
45
jest . mock ( '../chronos_npm_package/controllers/dockerHelper' , ( ) => ( {
61
46
...jest . requireActual ( '../chronos_npm_package/controllers/dockerHelper' ) ,
62
47
getDockerContainer : jest . fn ( ) ,
63
48
readDockerContainer : jest . fn ( ) ,
64
49
} ) ) ;
65
50
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
-
75
51
describe ( 'mongo.connect' , ( ) => {
76
52
beforeEach ( ( ) => {
77
53
jest . clearAllMocks ( ) ;
78
54
} ) ;
79
55
80
56
test ( 'should connect to MongoDB database' , async ( ) => {
81
- await mongo . connect ( { database : { URI : db } } ) ;
57
+ await mongo . connect ( { database : { URI : uri } } ) ;
82
58
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
+ ) ;
87
62
} ) ;
88
63
89
64
test ( 'should handle connection errors' , async ( ) => {
@@ -98,12 +73,14 @@ describe('mongo.connect', () => {
98
73
} ) ;
99
74
100
75
describe ( 'mongo.services' , ( ) => {
101
- beforeEach ( ( ) => {
76
+ beforeEach ( async ( ) => {
102
77
jest . clearAllMocks ( ) ;
78
+ await connectDB ( ) ;
103
79
} ) ;
104
80
105
81
afterEach ( async ( ) => {
106
82
await dropCollections ( ) ;
83
+ await dropDB ( ) ;
107
84
} ) ;
108
85
109
86
test ( 'should create a new document' , async ( ) => {
@@ -114,12 +91,14 @@ describe('mongo.services', () => {
114
91
} ) ;
115
92
116
93
describe ( 'mongo.communications' , ( ) => {
117
- beforeEach ( ( ) => {
94
+ beforeEach ( async ( ) => {
118
95
jest . clearAllMocks ( ) ;
96
+ await connectDB ( ) ;
119
97
} ) ;
120
98
121
99
afterEach ( async ( ) => {
122
100
await dropCollections ( ) ;
101
+ await dropDB ( ) ;
123
102
} ) ;
124
103
125
104
test ( 'should record request cycle and save communication to the database' , async ( ) => {
@@ -137,7 +116,7 @@ describe('mongo.communications', () => {
137
116
const middleware = mongo . communications ( { microservice : 'test3' , slack : null , email : null } ) ;
138
117
await middleware ( req , res , ( ) => { } ) ;
139
118
const savedCommunication = await CommunicationModel . findOne ( { microservice : 'test3' } ) ;
140
- expect ( savedCommunication ) . toBeDefined ( ) ; // The document should be defined if it exists
119
+ expect ( savedCommunication ) . toBeDefined ( ) ;
141
120
} ) ;
142
121
143
122
test ( 'should send an alert' , async ( ) => {
@@ -166,13 +145,16 @@ describe('mongo.communications', () => {
166
145
} ) ;
167
146
168
147
describe ( 'mongo.health' , ( ) => {
169
- beforeEach ( ( ) => {
148
+ beforeEach ( async ( ) => {
170
149
jest . clearAllMocks ( ) ;
171
150
jest . useFakeTimers ( ) ;
151
+ await connectDB ( ) ;
172
152
} ) ;
173
153
174
- afterEach ( ( ) => {
154
+ afterEach ( async ( ) => {
175
155
jest . clearAllTimers ( ) ;
156
+ await dropCollections ( ) ;
157
+ await dropDB ( ) ;
176
158
} ) ;
177
159
178
160
test ( 'should collect data after the set interval' , async ( ) => {
@@ -209,11 +191,12 @@ describe('mongo.docker', () => {
209
191
beforeEach ( async ( ) => {
210
192
jest . clearAllMocks ( ) ;
211
193
jest . useFakeTimers ( ) ;
194
+ await connectDB ( ) ;
212
195
} ) ;
213
196
214
197
afterEach ( async ( ) => {
215
198
await dropCollections ( ) ;
216
- jest . clearAllTimers ( ) ;
199
+ await dropDB ( ) ;
217
200
} ) ;
218
201
219
202
test ( 'should collect docker container information' , async ( ) => {
@@ -244,6 +227,5 @@ describe('mongo.docker', () => {
244
227
expect ( dockerHelper . getDockerContainer ) . toHaveBeenCalledWith ( microservice ) ;
245
228
jest . advanceTimersByTime ( 1000 ) ;
246
229
expect ( dockerHelper . readDockerContainer ) . toHaveBeenCalledWith ( mockContainerData ) ;
247
- //expect(mockContainerInfoInstance.create).toHaveBeenCalledWith(mockReadDockerContainerData);
248
230
} ) ;
249
231
} ) ;
0 commit comments