@kibibit/configit 1.0.0-beta.2 → 1.0.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,319 @@
1
+ /* eslint-disable no-undefined */
2
+ import fsExtra from 'fs-extra';
3
+ import { mockProcessExit } from 'jest-mock-process';
4
+
5
+ import { ConfigService, EFileFormats, IConfigServiceOptions } from './config.service';
6
+ import { PizzaConfig, ToppingEnum, ToppingsConfig } from './pizza.config.model.mock';
7
+
8
+ class PizzaConfigService extends ConfigService<PizzaConfig> {
9
+ constructor(passedConfig?: Partial<PizzaConfig>, options?: IConfigServiceOptions) {
10
+ super(PizzaConfig, passedConfig, options);
11
+ }
12
+ }
13
+
14
+ describe('Config Service', () => {
15
+ let configService: PizzaConfigService;
16
+ let mockExit;
17
+ beforeAll(() => {
18
+ configService = new PizzaConfigService({
19
+ NODE_ENV: 'test'
20
+ });
21
+ });
22
+
23
+ beforeEach(() => {
24
+ jest.clearAllMocks();
25
+ mockExit = mockProcessExit();
26
+
27
+ (fsExtra.writeJSONSync as jest.Mock).mockReturnValue(false);
28
+ (fsExtra.writeFileSync as jest.Mock).mockReturnValue(false);
29
+ (fsExtra.pathExistsSync as jest.Mock).mockReturnValue(true);
30
+ });
31
+
32
+ afterEach(() => {
33
+ mockExit.mockRestore();
34
+ });
35
+ test('Service creation', () => {
36
+ expect(configService).toBeDefined();
37
+ });
38
+
39
+ test('Service is a forced singleton', () => {
40
+ const configService2 = new PizzaConfigService();
41
+ expect(configService2).toBe(configService);
42
+ });
43
+
44
+ test('Service can be forced to be created again', () => {
45
+ const configService2 = new PizzaConfigService({
46
+ NODE_ENV: 'test',
47
+ toppings: [ ToppingEnum.Cheese ]
48
+ });
49
+ expect(configService2.mode).toBe('test');
50
+ expect(configService2).not.toBe(configService);
51
+ expect(configService2.config).toMatchSnapshot();
52
+ });
53
+
54
+ test('Service can initialize the config files with saveToFile or init param', () => {
55
+ (fsExtra.pathExistsSync as jest.Mock).mockReturnValue(false);
56
+ new PizzaConfigService({
57
+ saveToFile: true,
58
+ NODE_ENV: 'test'
59
+ });
60
+
61
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(2);
62
+ expect((fsExtra.writeJSONSync as jest.Mock).mock.calls[0]).toMatchSnapshot();
63
+ expect((fsExtra.writeJSONSync as jest.Mock).mock.calls[1]).toMatchSnapshot();
64
+ expect(mockExit).toHaveBeenCalledWith(0);
65
+ });
66
+
67
+ test('Service can SAVE the config files with saveToFile or init param', () => {
68
+ new PizzaConfigService({
69
+ saveToFile: true,
70
+ NODE_ENV: 'test',
71
+ toppings: [ ToppingEnum.Cheese ]
72
+ });
73
+
74
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(2);
75
+ expect((fsExtra.writeJSONSync as jest.Mock).mock.calls[0]).toMatchSnapshot();
76
+ expect(mockExit).toHaveBeenCalledWith(0);
77
+ expect((fsExtra.writeJSONSync as jest.Mock).mock.calls[1]).toMatchSnapshot();
78
+ expect(mockExit).toHaveBeenCalledWith(0);
79
+ });
80
+
81
+ test('Service can Save and CONVERT the config files with saveToFile or init param', () => {
82
+ new PizzaConfigService({
83
+ saveToFile: true,
84
+ convert: EFileFormats.yaml,
85
+ NODE_ENV: 'test',
86
+ toppings: [ ToppingEnum.Cheese ]
87
+ });
88
+
89
+ expect(fsExtra.writeFileSync).toHaveBeenCalledTimes(1);
90
+
91
+ const [ filePath, fileContent ] = (fsExtra.writeFileSync as jest.Mock).mock.calls[0];
92
+
93
+ expect(filePath).toMatchSnapshot();
94
+ expect(`\n${ fileContent }\n`).toMatchSnapshot();
95
+ expect(mockExit).toHaveBeenCalledWith(0);
96
+ });
97
+
98
+ describe('wrap variables in attribute', () => {
99
+ test('YAML', () => {
100
+ new PizzaConfigService({
101
+ saveToFile: true,
102
+ convert: EFileFormats.yaml,
103
+ wrapper: 'env_variables',
104
+ NODE_ENV: 'test',
105
+ toppings: [ ToppingEnum.Cheese ]
106
+ });
107
+
108
+ expect(fsExtra.writeFileSync).toHaveBeenCalledTimes(1);
109
+
110
+ const [ filePath, fileContent ] = (fsExtra.writeFileSync as jest.Mock).mock.calls[0];
111
+
112
+ expect(filePath).toMatchSnapshot();
113
+ expect(`\n${ fileContent }\n`).toMatchSnapshot();
114
+ expect(mockExit).toHaveBeenCalledWith(0);
115
+ });
116
+
117
+ test('JSONC', () => {
118
+ new PizzaConfigService({
119
+ saveToFile: true,
120
+ wrapper: 'env_variables',
121
+ NODE_ENV: 'test',
122
+ toppings: [ ToppingEnum.Cheese ]
123
+ }, { fileFormat: EFileFormats.jsonc });
124
+
125
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(2);
126
+
127
+ const [ filePath, fileContent ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[0];
128
+ const [ filePath2, fileContent2 ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[1];
129
+
130
+ expect(filePath).toMatchSnapshot();
131
+ expect(fileContent).toMatchSnapshot();
132
+ expect(mockExit).toHaveBeenCalledWith(0);
133
+
134
+ expect(filePath2).toMatchSnapshot();
135
+ expect(fileContent2).toMatchSnapshot();
136
+ expect(mockExit).toHaveBeenCalledWith(0);
137
+ });
138
+
139
+ test('JSON', () => {
140
+ new PizzaConfigService({
141
+ saveToFile: true,
142
+ wrapper: 'env_variables',
143
+ NODE_ENV: 'test',
144
+ toppings: [ ToppingEnum.Cheese ]
145
+ });
146
+
147
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(2);
148
+
149
+ const [ filePath, fileContent ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[0];
150
+ const [ filePath2, fileContent2 ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[1];
151
+
152
+ expect(filePath).toMatchSnapshot();
153
+ expect(fileContent).toMatchSnapshot();
154
+ expect(mockExit).toHaveBeenCalledWith(0);
155
+
156
+ expect(filePath2).toMatchSnapshot();
157
+ expect(fileContent2).toMatchSnapshot();
158
+ expect(mockExit).toHaveBeenCalledWith(0);
159
+ });
160
+ });
161
+
162
+ test('Service returns correct empty yaml when config is empty', () => {
163
+ new PizzaConfigService({
164
+ saveToFile: true,
165
+ convert: EFileFormats.yaml
166
+ });
167
+
168
+ expect(fsExtra.writeFileSync).toHaveBeenCalledTimes(1);
169
+
170
+ const [ filePath, fileContent ] = (fsExtra.writeFileSync as jest.Mock).mock.calls[0];
171
+
172
+ expect(filePath).toMatchSnapshot();
173
+ expect(fileContent.trim()).toMatchSnapshot();
174
+ expect(mockExit).toHaveBeenCalledWith(0);
175
+ });
176
+
177
+ test('Service can return a plain object', () => {
178
+ const pizzaConfigInstance = new PizzaConfigService({
179
+ NODE_ENV: 'test',
180
+ toppings: [ ToppingEnum.Cheese ]
181
+ });
182
+
183
+ const plainObjectConfig = pizzaConfigInstance.toPlainObject();
184
+ expect(plainObjectConfig).toMatchSnapshot();
185
+ });
186
+
187
+ test('Service can save yaml without schema', async () => {
188
+ const pizzaConfigInstance = new PizzaConfigService({
189
+ NODE_ENV: 'test',
190
+ toppings: [ ToppingEnum.Cheese ]
191
+ });
192
+
193
+ await pizzaConfigInstance.writeConfigToFile({
194
+ fileFormat: EFileFormats.yaml,
195
+ excludeSchema: true
196
+ });
197
+
198
+ expect(fsExtra.writeFileSync).toHaveBeenCalledTimes(1);
199
+
200
+ const [ filePath, fileContent ] = (fsExtra.writeFileSync as jest.Mock).mock.calls[0];
201
+
202
+ expect(filePath).toMatchSnapshot();
203
+ expect(fileContent).not.toContain('# yaml-language-server: $schema=');
204
+ expect(fileContent).toMatchSnapshot();
205
+ });
206
+
207
+ test('writeConfigToFile YAML', () => {
208
+ const config = new PizzaConfigService({
209
+ toppings: [ ToppingEnum.Cheese ]
210
+ });
211
+
212
+ config.writeConfigToFile({
213
+ fileFormat: EFileFormats.yaml,
214
+ excludeSchema: true,
215
+ objectWrapper: 'env_variables'
216
+ });
217
+
218
+ expect(fsExtra.writeFileSync).toHaveBeenCalledTimes(1);
219
+
220
+ const [ filePath, fileContent ] = (fsExtra.writeFileSync as jest.Mock).mock.calls[0];
221
+
222
+ expect(filePath).toMatchSnapshot();
223
+ expect(fileContent.trim()).toMatchSnapshot();
224
+ });
225
+
226
+ test('writeConfigToFile HJSON', () => {
227
+ const config = new PizzaConfigService({
228
+ toppings: [ ToppingEnum.Cheese ]
229
+ });
230
+
231
+ config.writeConfigToFile({
232
+ fileFormat: EFileFormats.hjson,
233
+ excludeSchema: true,
234
+ objectWrapper: 'env_variables'
235
+ });
236
+
237
+ expect(fsExtra.writeFileSync).toHaveBeenCalledTimes(1);
238
+
239
+ const [ filePath, fileContent ] = (fsExtra.writeFileSync as jest.Mock).mock.calls[0];
240
+
241
+ expect(filePath).toMatchSnapshot();
242
+ expect(fileContent.trim()).toMatchSnapshot();
243
+ });
244
+
245
+ test('writeConfigToFile JSONC', () => {
246
+ const config = new PizzaConfigService({
247
+ toppings: [ ToppingEnum.Cheese ]
248
+ });
249
+
250
+ jest.clearAllMocks();
251
+
252
+ config.writeConfigToFile({
253
+ fileFormat: EFileFormats.jsonc,
254
+ excludeSchema: true,
255
+ objectWrapper: 'env_variables'
256
+ });
257
+
258
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(1);
259
+
260
+ const [ filePath, fileContent ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[0];
261
+
262
+ expect(filePath).toMatchSnapshot();
263
+ expect(fileContent).toMatchSnapshot();
264
+ });
265
+
266
+ test('writeConfigToFile JSON', () => {
267
+ const config = new PizzaConfigService({
268
+ toppings: [ ToppingEnum.Cheese ]
269
+ });
270
+
271
+ jest.clearAllMocks();
272
+
273
+ config.writeConfigToFile({
274
+ fileFormat: EFileFormats.json,
275
+ excludeSchema: true,
276
+ objectWrapper: 'env_variables'
277
+ });
278
+
279
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(1);
280
+
281
+ const [ filePath, fileContent ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[0];
282
+
283
+ expect(filePath).toMatchSnapshot();
284
+ expect(fileContent).toMatchSnapshot();
285
+ });
286
+
287
+ describe('Shared Configurations', () => {
288
+ test('Can define shared config', () => {
289
+ const pizzaConfigInstance = new PizzaConfigService({
290
+ NODE_ENV: 'test',
291
+ toppings: [ ToppingEnum.Cheese ],
292
+ INCLUDE_MEAT: true
293
+ } as any, {
294
+ sharedConfig: [ ToppingsConfig ]
295
+ });
296
+
297
+ pizzaConfigInstance.writeConfigToFile({
298
+ fileFormat: EFileFormats.json
299
+ // excludeSchema: false
300
+ });
301
+
302
+ expect(fsExtra.writeJSONSync).toHaveBeenCalledTimes(4);
303
+
304
+ const [ filePath0, fileContent0 ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[0];
305
+ const [ filePath1, fileContent1 ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[1];
306
+ const [ filePath2, fileContent2 ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[2];
307
+ const [ filePath3, fileContent3 ] = (fsExtra.writeJSONSync as jest.Mock).mock.calls[3];
308
+
309
+ expect(filePath0).toMatchSnapshot();
310
+ expect(fileContent0).toMatchSnapshot();
311
+ expect(filePath1).toMatchSnapshot();
312
+ expect(fileContent1).toMatchSnapshot();
313
+ expect(filePath2).toMatchSnapshot();
314
+ expect(fileContent2).toMatchSnapshot();
315
+ expect(filePath3).toMatchSnapshot();
316
+ expect(fileContent3).toMatchSnapshot();
317
+ });
318
+ });
319
+ });