-
Notifications
You must be signed in to change notification settings - Fork 1
/
typed-env-integration.spec.ts
1022 lines (860 loc) · 28.4 KB
/
typed-env-integration.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { EnvBoolean, EnvEnum, EnvFloat, EnvInteger, Environment, EnvNested, EnvString, loadEnvConfig } from './index';
import { resetMetadataStorage } from './metadata-storage';
import { EnvRawObject, Inter, Type } from './types';
import {
EnvPropConfigError,
EnvPropDecorationError,
EnvVarNameDuplicateError,
NoEnvVarError,
TypeCastingError,
} from './errors';
describe('Typed Env: Integration test', () => {
beforeEach(() => {
resetMetadataStorage();
});
it('Should retrieve all simple types from the ENV object', () => {
// arrange
enum ETest {
One = 'one',
Two = 'two',
}
const symbolKey = Symbol('Env Symbol');
const symbolWithDefaultKey = Symbol('Env Symbol With Default');
const symbolOptionalKey = Symbol('Env Symbol Optional');
class Config {
@EnvInteger()
public readonly int!: number;
@EnvInteger()
public readonly intWithDefault: number = 123;
@EnvInteger({ optional: true })
public readonly intOptional?: number;
@EnvFloat()
public readonly float!: number;
@EnvFloat()
public readonly floatWithDefault: number = 123.45;
@EnvFloat({ optional: true })
public readonly floatOptional?: number;
@EnvString()
public readonly str!: string;
@EnvString()
public readonly strWithDefault: string = 'hello';
@EnvString({ optional: true })
public readonly strOptional?: string;
@EnvBoolean()
public readonly bool!: boolean;
@EnvBoolean()
public readonly boolWithDefault: boolean = true;
@EnvBoolean({ optional: true })
public readonly boolOptional?: boolean;
@EnvEnum({ enum: ETest })
public readonly enum!: ETest;
@EnvEnum({ enum: ETest })
public readonly enumWithDefault: ETest = ETest.One;
@EnvEnum({ enum: ETest, optional: true })
public readonly enumOptional?: ETest;
@EnvInteger({ name: 'SYMBOL' })
public readonly [symbolKey]!: number;
@EnvInteger({ name: 'SYMBOL_WITH_DEFAULT' })
public readonly [symbolWithDefaultKey]: number = 234;
// TODO: This case doesn't work. It looks like there is a problem with Jest
// https://github.com/facebook/jest/issues/8475
// @EnvInteger({ name: 'SYMBOL_OPTIONAL', optional: true })
public readonly [symbolOptionalKey]?: number;
public readonly justValue: string = 'Z-Brain';
}
const raw: EnvRawObject = {
INT: '111',
FLOAT: '111.222',
STR: 'welcome',
BOOL: '0',
ENUM: 'two',
SYMBOL: '345',
};
const expected: Inter<Config> = {
int: 111,
intWithDefault: 123,
intOptional: undefined,
float: 111.222,
floatWithDefault: 123.45,
floatOptional: undefined,
str: 'welcome',
strWithDefault: 'hello',
strOptional: undefined,
bool: false,
boolWithDefault: true,
boolOptional: undefined,
enum: ETest.Two,
enumWithDefault: ETest.One,
enumOptional: undefined,
[symbolKey]: 345,
[symbolWithDefaultKey]: 234,
// [symbolOptionalKey]: undefined,
justValue: 'Z-Brain',
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
}); // END Should retrieve all simple types from the ENV object
describe('Instantiating', () => {
it('Should the same object reference in case we are passing an instance instead of a constructor', () => {
// arrange
class Config {
@EnvString()
public readonly name!: string;
}
const config = new Config();
const raw: EnvRawObject = { NAME: 'hello' };
const expected: Config = { name: 'hello' };
// act
const res = loadEnvConfig(config, raw);
// assert
expect(res).toBe(config);
expect(res).toEqual(expected);
});
describe(`@${ Environment.name }() decorator`, () => {
it(`Should be filled during instantiating without ${ loadEnvConfig.name }() call`, () => {
// arrange
const raw: EnvRawObject = { NAME: 'hello' };
@Environment(() => raw)
class Config {
@EnvString()
public readonly name!: string;
}
const expected: Config = { name: 'hello' };
// act
const config = new Config();
// assert
expect(config).toEqual(expected);
});
it('Should use process.env in case rawFactory argument skipped', () => {
@Environment()
class Config {
@EnvString()
public readonly name!: string;
}
const expected: Config = { name: 'hello' };
process.env.NAME = 'hello';
// act
const config = new Config();
// assert
expect(config).toEqual(expected);
// clean up
const { NAME: deleted, ...env } = process.env; // eslint-disable-line @typescript-eslint/no-unused-vars
process.env = env;
});
it('Should name the class using the original config class name prefixed by double dollar', () => {
// arrange
@Environment()
class Config {
@EnvString()
public readonly name: string = '';
}
// act
const config = new Config();
// assert
expect(Config.name).toBe('$$Config');
expect(config.constructor.name).toBe('$$Config');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(Object.getPrototypeOf(Config.prototype).constructor.name).toBe('Config');
});
it('Should pass the wrapped constructor arguments to the original constructor during instantiating', () => {
// arrange
const raw: EnvRawObject = { NAME: 'hello' };
@Environment(() => raw)
class Config {
@EnvString()
public readonly name!: string;
public readonly fromCtor: string;
public constructor(
public readonly arg: string,
) {
this.fromCtor = `${ arg } 2`;
}
}
const expected: Config = {
name: 'hello',
arg: 'welcome',
fromCtor: 'welcome 2',
};
// act
const config = new Config('welcome');
// assert
expect(config).toEqual(expected);
});
}); // END `@${ Environment.name }() decorator`
}); // END Instantiating
describe('Handling array in values', () => {
it('Should handle arrays of all simple types', () => {
// arrange
class Config {
@EnvInteger()
public readonly array!: number[];
// reflect-metadata was unable to reflect Array<...> as an array in the old TS versions
@EnvInteger()
public readonly arrayCtor!: Array<number>;
@EnvInteger()
public readonly arrayWithDefault: number[] = [1, 2, 3];
@EnvInteger({ optional: true })
public readonly arrayOptional!: number[];
@EnvFloat()
public readonly floatArray!: number[];
@EnvString()
public readonly strArray!: string[];
@EnvBoolean()
public readonly boolArray!: boolean[];
}
const raw: EnvRawObject = {
ARRAY: '11,22,33',
ARRAY_CTOR: '1,2,3',
FLOAT_ARRAY: '11.22,33.44,55.66',
STR_ARRAY: 'me,you,we',
BOOL_ARRAY: 'true,0,false,1',
};
const expected: Inter<Config> = {
array: [11, 22, 33],
arrayCtor: [1, 2, 3],
arrayWithDefault: [1, 2, 3],
arrayOptional: [],
floatArray: [11.22, 33.44, 55.66],
strArray: ['me', 'you', 'we'],
boolArray: [true, false, false, true],
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
}); // END Should handle arrays of all simple types
it('Should not split by escaped commas during paring arrays', () => {
// arrange
class Config {
@EnvString()
public readonly names!: string[];
}
const raw: EnvRawObject = {
NAMES: 'Ivan\\,First,Petro\\,Second',
};
const expected: Config = {
names: ['Ivan,First', 'Petro,Second'],
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
});
it('Should handle escaped backslash before commas during paring arrays', () => {
// arrange
class Config {
@EnvString()
public readonly names!: string[];
}
const raw: EnvRawObject = {
NAMES: 'Ivan\\\\,First,Petro\\,Second',
};
const expected: Config = {
names: ['Ivan\\', 'First', 'Petro,Second'],
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
});
}); // END Handling array in values
it('Should use an array of specified ENV variable names to find existing one', () => {
// arrange
class Config {
@EnvString({ name: ['TEST1', 'TEST2', 'TEST3'] })
public readonly test!: string;
// Loader should iterate the array of names in the order from the first to the last
@EnvInteger({ name: ['FIRST3', 'FIRST2', 'FIRST1'] })
public readonly first!: number;
}
const raw: EnvRawObject = {
TEST2: 'hello',
FIRST1: '111',
FIRST2: '222',
FIRST3: '333',
};
const expected: Inter<Config> = {
test: 'hello',
first: 333,
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
}); // END Should use an array of specified ENV variable names to find existing one
describe('.allowEmpty flag', () => {
const rawEmptyStr: EnvRawObject = { NAME: '' };
const rawSpaceOnlyStr: EnvRawObject = { NAME: ' ' };
it('Should not allow empty values by default', () => {
// arrange
class Config {
@EnvString()
public readonly name!: string;
}
// act
const onEmptyCb = () => loadEnvConfig(Config, rawEmptyStr);
const onSpaceOnlyCb = () => loadEnvConfig(Config, rawSpaceOnlyStr);
// assert
expect(onEmptyCb).toThrowError('"NAME" is required');
expect(onSpaceOnlyCb).toThrowError('"NAME" is required');
});
it('Should not allow empty values in case .allowEmpty is true', () => {
// arrange
class Config {
@EnvString({ allowEmpty: true })
public readonly name!: string;
}
// act
const resOnEmpty = loadEnvConfig(Config, rawEmptyStr);
const resOnSpaces = loadEnvConfig(Config, rawSpaceOnlyStr);
// assert
expect(resOnEmpty).toEqual({ name: '' });
expect(resOnSpaces).toEqual({ name: ' ' });
});
}); // END .allowEmpty flag
describe('Nested Configs', () => {
it('Should retrieve DTO metadata on the all inheritance level', () => {
// arrange
class Base {
@EnvString()
public readonly name!: string;
}
class Child extends Base {
@EnvString()
public readonly email!: string;
}
class Main extends Child {
@EnvInteger()
public readonly age!: number;
}
const raw: EnvRawObject = {
NAME: 'welcome',
EMAIL: '[email protected]',
AGE: '25',
};
const expected: Main = {
name: 'welcome',
email: '[email protected]',
age: 25,
};
// act
const config = loadEnvConfig(Main, raw);
// assert
expect(config).toEqual(expected);
});
it('Should throw a RangeError on to long inheritance chain', () => {
// arrange
let ctor = class My {};
const ACTUAL_LIMIT = 15;
const LEVEL = 20;
// making 20-level inheritance (limit is 15)
for (let i = 1; i <= LEVEL; i++) {
// eslint-disable-next-line @typescript-eslint/no-implied-eval,no-new-func
ctor = new Function('Prev', `return class Next${ i } extends Prev {}`)(ctor) as Type<any>;
}
// act
const cb = () => {
loadEnvConfig(ctor, {});
};
// assert
const exp = expect(cb);
exp.toThrowError(RangeError);
exp.toThrowError(String(ACTUAL_LIMIT));
exp.toThrowError('ENV_CONFIG_MAX_INHERITANCE_LIMIT');
exp.toThrowError(`Next${ LEVEL - ACTUAL_LIMIT }`);
});
it('Should handle simple nested config', () => {
// arrange
class NestedConfig {
@EnvInteger()
public readonly name!: number;
}
class Config {
@EnvString()
public readonly name!: string;
@EnvNested()
public readonly deep!: NestedConfig;
}
const raw: EnvRawObject = {
NAME: 'car',
DEEP_NAME: '111',
};
const expected: Inter<Config> = {
name: 'car',
deep: {
name: 111,
},
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
});
it('Should handle custom prefix for nested config', () => {
// arrange
class NestedConfig {
@EnvInteger()
public readonly name!: number;
}
class Config {
@EnvString()
public readonly name!: string;
@EnvNested({ prefix: 'MY' })
public readonly deep!: NestedConfig;
}
const raw: EnvRawObject = {
NAME: 'car',
MY_NAME: '111',
};
const expected: Inter<Config> = {
name: 'car',
deep: {
name: 111,
},
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
});
it('Should not add a name prefix in case the .prefix param assigned with `false`', () => {
// arrange
class NestedConfig {
@EnvInteger()
public readonly age!: number;
}
class Config {
@EnvString()
public readonly name!: string;
@EnvNested({ prefix: false })
public readonly deep!: NestedConfig;
}
const raw: EnvRawObject = {
NAME: 'car',
AGE: '111',
};
const expected: Inter<Config> = {
name: 'car',
deep: {
age: 111,
},
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
});
it('Should handle multiple nesting and concatenate prefixes', () => {
// arrange
class Nested2Config {
@EnvString({ allowConflictingVarName: true })
public readonly name!: string;
}
class NestedConfig {
@EnvFloat({ allowConflictingVarName: true })
public readonly name!: number;
@EnvNested()
public readonly nested!: Nested2Config;
@EnvNested({ prefix: false })
public readonly noPrefix!: Nested2Config;
}
class Config {
@EnvInteger()
public readonly name!: number;
@EnvNested()
public readonly deep!: NestedConfig;
}
const raw: EnvRawObject = {
NAME: '111',
DEEP_NAME: '11.22',
DEEP_NESTED_NAME: 'car',
};
const expected: Inter<Config> = {
name: 111,
deep: {
name: 11.22,
nested: { name: 'car' },
noPrefix: { name: '11.22' },
},
};
// act
const config = loadEnvConfig(Config, raw);
// assert
expect(config).toEqual(expected);
});
}); // END Nested Configs
describe('Mixed types/Multi-type/Multi-decorator properties', () => {
describe('GIVEN: 3 decorators added: EnvInteger/EnvBoolean/EnvString', () => {
it('Should choose the right type by the input data', () => {
// arrange
class Config {
@EnvString()
@EnvBoolean()
@EnvInteger()
public mixed!: number | boolean | string;
}
const rawInteger: EnvRawObject = { MIXED: '1234' };
const rawBoolean: EnvRawObject = { MIXED: 'yes' };
const rawString: EnvRawObject = { MIXED: 'hello' };
// act
const resInteger = loadEnvConfig(Config, rawInteger);
const resBoolean = loadEnvConfig(Config, rawBoolean);
const resString = loadEnvConfig(Config, rawString);
// assert
expect(resInteger.mixed).toBe(1234);
expect(resBoolean.mixed).toBe(true);
expect(resString.mixed).toBe('hello');
});
});
describe('GIVEN: 2 decorators added: EnvInteger/EnvBoolean', () => {
it('Should throw an error in case passed string is not an integer & not a boolean-like', () => {
// arrange
class Config {
@EnvBoolean()
@EnvInteger()
public mixed!: number | boolean | string;
}
const rawString: EnvRawObject = { MIXED: 'hello' };
// act
const cb = () => loadEnvConfig(Config, rawString);
// assert
const exp = expect(cb);
exp.toThrowError('No acceptable value for multi-type field');
exp.toThrowError('For INTEGER');
exp.toThrowError('For BOOLEAN');
});
});
describe('GIVEN: 2 decorators added: EnvInteger/EnvNested', () => {
it('Should throw an error in case passed string is not an integer & not the nested config', () => {
// arrange
class NestedConfig {
@EnvString()
public name!: string;
}
class Config {
@EnvInteger()
@EnvNested({ config: NestedConfig })
public mixed!: number | NestedConfig;
}
const rawString: EnvRawObject = { MIXED: 'hello' };
// act
const cb = () => loadEnvConfig(Config, rawString);
// assert
const exp = expect(cb);
exp.toThrowError('No acceptable value for multi-type field');
exp.toThrowError('For INTEGER');
exp.toThrowError('For NESTED');
});
it('Should make an integer value from integer-like value', () => {
// arrange
class NestedConfig {
@EnvString()
public name!: string;
}
class Config {
@EnvInteger()
@EnvNested({ config: NestedConfig })
public mixed!: number | NestedConfig;
}
const rawString: EnvRawObject = { MIXED: '1234' };
// act
const res = loadEnvConfig(Config, rawString);
// assert
expect(res.mixed).toBe(1234);
});
it('Should make a nested-object in case an integer-like ENV variable is absent, a var for nested config exists', () => {
// arrange
class NestedConfig {
@EnvString()
public name!: string;
}
class Config {
@EnvInteger()
@EnvNested({ config: NestedConfig })
public mixed!: number | NestedConfig;
}
const rawString: EnvRawObject = { MIXED_NAME: 'hello' };
// act
const res = loadEnvConfig(Config, rawString);
// assert
expect((res.mixed as NestedConfig).name).toBe('hello');
});
});
describe('Receiving .isArray param to handle arrays in mixed types', () => {
it('Should handle isArray flag for arrays of simple types', () => {
// arrange
class Config {
@EnvFloat()
@EnvInteger({ isArray: true })
@EnvBoolean({ isArray: true })
public mixed!: boolean[] | number[] | number;
}
const rawInt: EnvRawObject = { MIXED: '123,234' };
const rawBool: EnvRawObject = { MIXED: 'true,false,true' };
const rawFloat: EnvRawObject = { MIXED: '123.2' };
// act
const resInt = loadEnvConfig(Config, rawInt);
const resBool = loadEnvConfig(Config, rawBool);
const resFloat = loadEnvConfig(Config, rawFloat);
// assert
expect(resInt.mixed).toEqual([123, 234]);
expect(resBool.mixed).toEqual([true, false, true]);
expect(resFloat.mixed).toBe(123.2);
});
it('Should handle isArray flag for a mix with simple & nested types', () => {
// arrange
class NestedConfig {
@EnvString()
public name!: string;
}
class Config {
@EnvNested({ config: NestedConfig })
@EnvBoolean({ isArray: true })
public mixed!: boolean[] | NestedConfig;
}
const rawNested: EnvRawObject = { MIXED_NAME: 'hello' };
const rawBool: EnvRawObject = { MIXED: 'true,false,true' };
// act
const resBool = loadEnvConfig(Config, rawBool);
const resNested = loadEnvConfig(Config, rawNested);
// assert
expect(resBool.mixed).toEqual([true, false, true]);
expect(resNested.mixed).toEqual({ name: 'hello' });
});
});
describe(`GIVEN: A property with triple-mix type: boolean, string enum & array of string enum values
THEN: Should cast all of them`, () => {
// arrange
/** This is a complex type from TypeORM, it is copy-pasted just to make the test more realistic */
type LoggerOptions = boolean | 'all' | ('query' | 'schema' | 'error' | 'warn' | 'info' | 'log' | 'migration')[];
const allowedValues: LoggerOptions = ['query', 'schema', 'error', 'warn', 'info', 'log', 'migration'];
class Config {
@EnvBoolean()
@EnvEnum({ enum: allowedValues, isArray: true })
@EnvEnum({ enum: ['all'] })
public logging!: LoggerOptions;
}
const rawBool: EnvRawObject = { LOGGING: 'true' };
const rawAll: EnvRawObject = { LOGGING: 'all' };
const rawEnum: EnvRawObject = { LOGGING: 'error,warn,info' };
// act
const resBool = loadEnvConfig(Config, rawBool);
const resAll = loadEnvConfig(Config, rawAll);
const resEnum = loadEnvConfig(Config, rawEnum);
// assert
expect(resBool.logging).toBe(true);
expect(resAll.logging).toBe('all');
expect(resEnum.logging).toEqual(['error', 'warn', 'info']);
});
}); // END Multi-type/Multi-decorator properties
describe('Freezing config', () => {
it('Should freeze top level config', () => {
// arrange
class Config {
@EnvString()
public name!: string;
}
const raw: EnvRawObject = {
NAME: 'hello',
};
const config = loadEnvConfig(Config, raw);
// act
const cb = () => { config.name = 'test'; };
// assert
expect(cb).toThrowError();
});
it('Should freeze nested configs', () => {
// arrange
class Nested {
@EnvInteger()
public age!: number;
}
class Config {
@EnvString()
public name!: string;
@EnvNested()
public readonly deep!: Nested;
}
const raw: EnvRawObject = {
NAME: 'hello',
DEEP_AGE: '25',
};
const config = loadEnvConfig(Config, raw);
// act
const cb = () => { config.deep.age = 35; };
// assert
expect(cb).toThrowError();
});
}); // END Freezing config
describe('Failed scenarios/Error messages and types testing', () => {
describe(`${ EnvPropConfigError.name }`, () => {
it('Should throw the error when a property defined using a symbol and custom name is not defined', () => {
// arrange
const key = Symbol('My Key');
class Config {
@EnvString()
public readonly [key]!: string;
}
// act
const cb = () => {
loadEnvConfig(Config, {});
};
// assert
const exp = expect(cb);
exp.toThrowError(EnvPropConfigError);
exp.toThrowError('Config.Symbol(My Key)');
});
it('Should throw the error when duplicate names specified in the custom "name" param', () => {
// arrange
class Config {
@EnvString(['AAA', 'BBB', 'AAA', 'CCC'])
public readonly name!: string;
}
const raw: EnvRawObject = { AAA: '111', BBB: '222', CCC: '333' };
// act
const cb = () => {
loadEnvConfig(Config, raw);
};
// assert
expect(cb).toThrowError(/Config\.name.+AAA.+BBB.+CCC/);
});
}); // END EnvPropConfigError
describe(`${ NoEnvVarError.name }`, () => {
it('Should throw the error when an env variable with the only one name is missed', () => {
// arrange
class Config {
@EnvString()
public readonly myName!: string;
}
// act
const cb = () => {
loadEnvConfig(Config, {});
};
// assert
const exp = expect(cb);
exp.toThrowError(NoEnvVarError);
exp.toThrowError('Config.myName');
exp.toThrowError('"MY_NAME"');
});
it('Should throw the error when an env variable with multiple one names is missed', () => {
// arrange
class Config {
@EnvString(['one', 'two'])
public readonly myName!: string;
}
// act
const cb = () => {
loadEnvConfig(Config, {});
};
// assert
const exp = expect(cb);
exp.toThrowError(NoEnvVarError);
exp.toThrowError('Config.myName');
exp.toThrowError('"ONE"');
exp.toThrowError('"TWO"');
});
}); // END NoEnvVarError
describe(`${ TypeCastingError.name }`, () => {
it('Should throw the error on casting being failed', () => {
// arrange
enum EKind {
One = 'one',
Two = 'two',
}
class Config {
@EnvEnum(EKind)
public readonly kind!: EKind[];
}
const raw: EnvRawObject = {
KIND: 'three',
};
// act
const cb = () => {
loadEnvConfig(Config, raw);
};
// assert
const exp = expect(cb);
exp.toThrowError(TypeCastingError);
exp.toThrowError('Config.kind'); // field name
exp.toThrowError('"KIND"'); // ENV var name
exp.toThrowError('Is Array: true'); // is array flag
exp.toThrowError(/"One": "one"(.|\n)+"Two": "two"/); // stringified enum
exp.toThrowError(/\(string\).+"three"/); // actual value and it's type
exp.toThrowError(/^[^ ].+\n( {4}[^ ].+\n)*$/g); // indentation
});
}); // END TypeCastingError
describe(`${ EnvPropDecorationError.name }`, () => {
it('Should the error on nested config type is not a class and custom type(decorator param) is missed', () => {
// arrange
interface NestedInter {
name: string;
}
// act
const cb = () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class Config {
@EnvNested()
public readonly deep!: NestedInter;
}
};
// assert
const exp = expect(cb);
exp.toThrowError(EnvPropDecorationError);
exp.toThrowError('Config.deep');
exp.toThrowError(`${ EnvNested.name }`);
});
}); // END EnvPropDecorationError
describe(`${ EnvVarNameDuplicateError.name }`, () => {
const raw: EnvRawObject = Object.freeze({
NAME: 'hello',
MY_NAME: 'welcome',
});
it('Should throw the error on duplicate fields being specified', () => {
// arrange
class Nested {
@EnvString()
public readonly name!: string;
}
class Config {
@EnvString('MY_NAME')
public readonly name!: string;
@EnvNested()
public readonly my!: Nested;
}
// act
const cb = () => {
loadEnvConfig(Config, raw);
};
// assert
const exp = expect(cb);
exp.toThrowError('Config.name');
exp.toThrowError('Nested.name');
exp.toThrowError('MY_NAME');
exp.toThrowError('allowConflictingVarName');
});
it('Should not throw the error when duplicate fields being specified .allowConflictingVarName = true', () => {
// arrange
class Nested {
@EnvString({ allowConflictingVarName: true })
public readonly name!: string;
}
class Config {