@@ -2,29 +2,13 @@ const mongoose = require('mongoose');
2
2
const mongo = require ( '../chronos_npm_package/controllers/mongo' ) ;
3
3
const ServicesModel = require ( '../chronos_npm_package/models/ServicesModel' ) ;
4
4
const CommunicationModel = require ( '../chronos_npm_package/models/CommunicationModel' ) ;
5
- const { connectDB, dropDB, dropCollections } = require ( './mockdbsetup' ) ;
5
+ const { connectDB, dropDB, dropCollections, uri } = require ( './mockdbsetup' ) ;
6
6
const alert = require ( '../chronos_npm_package/controllers/alert' ) ;
7
- const ContainerInfoFunc = require ( '../chronos_npm_package/models/ContainerInfo' ) ;
8
7
const dockerHelper = require ( '../chronos_npm_package/controllers/dockerHelper' ) ;
9
8
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
-
22
9
jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
23
-
24
10
jest . mock ( '../chronos_npm_package/controllers/alert' ) ;
25
-
26
11
jest . useFakeTimers ( ) ;
27
-
28
12
jest . spyOn ( global , 'setInterval' ) ;
29
13
30
14
jest . mock ( '../chronos_npm_package/controllers/healthHelpers' , ( ) => {
@@ -53,31 +37,22 @@ jest.mock('../chronos_npm_package/controllers/mongo', () => ({
53
37
const HealthModel = {
54
38
insertMany : jest . fn ( ( ) => Promise . resolve ( ) ) ,
55
39
} ;
40
+ const HealthModelFunc = jest . fn ( ( ) => HealthModel ) ;
56
41
57
42
jest . mock ( '../chronos_npm_package/controllers/dockerHelper' , ( ) => ( {
58
43
...jest . requireActual ( '../chronos_npm_package/controllers/dockerHelper' ) ,
59
44
getDockerContainer : jest . fn ( ) ,
60
45
readDockerContainer : jest . fn ( ) ,
61
46
} ) ) ;
62
47
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
-
72
48
describe ( 'mongo.connect' , ( ) => {
73
49
beforeEach ( ( ) => {
74
50
jest . clearAllMocks ( ) ;
75
51
} ) ;
76
52
77
53
test ( 'should connect to MongoDB database' , async ( ) => {
78
- await mongo . connect ( { database : { URI : db } } ) ;
54
+ await mongo . connect ( { database : { URI : uri } } ) ;
79
55
80
- //expect(mongoose.connect).toHaveBeenCalledWith(db);
81
56
expect ( console . log ) . toHaveBeenCalledWith (
82
57
expect . stringContaining ( 'MongoDB database connected at' )
83
58
) ;
@@ -95,12 +70,14 @@ describe('mongo.connect', () => {
95
70
} ) ;
96
71
97
72
describe ( 'mongo.services' , ( ) => {
98
- beforeEach ( ( ) => {
73
+ beforeEach ( async ( ) => {
99
74
jest . clearAllMocks ( ) ;
75
+ await connectDB ( ) ;
100
76
} ) ;
101
77
102
78
afterEach ( async ( ) => {
103
79
await dropCollections ( ) ;
80
+ await dropDB ( ) ;
104
81
} ) ;
105
82
106
83
test ( 'should create a new document' , async ( ) => {
@@ -111,12 +88,14 @@ describe('mongo.services', () => {
111
88
} ) ;
112
89
113
90
describe ( 'mongo.communications' , ( ) => {
114
- beforeEach ( ( ) => {
91
+ beforeEach ( async ( ) => {
115
92
jest . clearAllMocks ( ) ;
93
+ await connectDB ( ) ;
116
94
} ) ;
117
95
118
96
afterEach ( async ( ) => {
119
97
await dropCollections ( ) ;
98
+ await dropDB ( ) ;
120
99
} ) ;
121
100
122
101
test ( 'should record request cycle and save communication to the database' , async ( ) => {
@@ -134,7 +113,7 @@ describe('mongo.communications', () => {
134
113
const middleware = mongo . communications ( { microservice : 'test3' , slack : null , email : null } ) ;
135
114
await middleware ( req , res , ( ) => { } ) ;
136
115
const savedCommunication = await CommunicationModel . findOne ( { microservice : 'test3' } ) ;
137
- expect ( savedCommunication ) . toBeDefined ( ) ; // The document should be defined if it exists
116
+ expect ( savedCommunication ) . toBeDefined ( ) ;
138
117
} ) ;
139
118
140
119
test ( 'should send an alert' , async ( ) => {
@@ -163,13 +142,16 @@ describe('mongo.communications', () => {
163
142
} ) ;
164
143
165
144
describe ( 'mongo.health' , ( ) => {
166
- beforeEach ( ( ) => {
145
+ beforeEach ( async ( ) => {
167
146
jest . clearAllMocks ( ) ;
168
147
jest . useFakeTimers ( ) ;
148
+ await connectDB ( ) ;
169
149
} ) ;
170
150
171
- afterEach ( ( ) => {
151
+ afterEach ( async ( ) => {
172
152
jest . clearAllTimers ( ) ;
153
+ await dropCollections ( ) ;
154
+ await dropDB ( ) ;
173
155
} ) ;
174
156
175
157
test ( 'should collect data after the set interval' , async ( ) => {
@@ -206,11 +188,12 @@ describe('mongo.docker', () => {
206
188
beforeEach ( async ( ) => {
207
189
jest . clearAllMocks ( ) ;
208
190
jest . useFakeTimers ( ) ;
191
+ await connectDB ( ) ;
209
192
} ) ;
210
193
211
194
afterEach ( async ( ) => {
212
195
await dropCollections ( ) ;
213
- jest . clearAllTimers ( ) ;
196
+ await dropDB ( ) ;
214
197
} ) ;
215
198
216
199
test ( 'should collect docker container information' , async ( ) => {
@@ -241,6 +224,5 @@ describe('mongo.docker', () => {
241
224
expect ( dockerHelper . getDockerContainer ) . toHaveBeenCalledWith ( microservice ) ;
242
225
jest . advanceTimersByTime ( 1000 ) ;
243
226
expect ( dockerHelper . readDockerContainer ) . toHaveBeenCalledWith ( mockContainerData ) ;
244
- //expect(mockContainerInfoInstance.create).toHaveBeenCalledWith(mockReadDockerContainerData);
245
227
} ) ;
246
228
} ) ;
0 commit comments