|
| 1 | +/* eslint-disable max-classes-per-file */ |
| 2 | +import { createMock, DeepMocked } from '@golevelup/ts-jest'; |
| 3 | +import { ConfigService } from '@nestjs/config'; |
| 4 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 5 | +import { IsBoolean, IsString } from 'class-validator'; |
| 6 | +import { StringToBoolean } from '../../shared/transformer/index.js'; |
| 7 | +import { ConfigProperty, Configuration } from './configuration.decorator.js'; |
| 8 | +import { ConfigurationFactory } from './configuration.factory.js'; |
| 9 | + |
| 10 | +@Configuration() |
| 11 | +class TestConfig { |
| 12 | + @IsString() |
| 13 | + @ConfigProperty('TEST_VALUE_1') |
| 14 | + public valueWithOtherEnvVar!: string; |
| 15 | + |
| 16 | + @IsString() |
| 17 | + @ConfigProperty('TEST_VALUE2') |
| 18 | + public valueWithOtherEnvVarAndDefault = 'default'; |
| 19 | + |
| 20 | + @IsString() |
| 21 | + @ConfigProperty() |
| 22 | + public valueWithoutDefault!: string; |
| 23 | + |
| 24 | + @IsBoolean() |
| 25 | + @StringToBoolean() |
| 26 | + @ConfigProperty() |
| 27 | + public valueWithoutDefaultBoolean!: boolean; |
| 28 | + |
| 29 | + @IsBoolean() |
| 30 | + public valueWithDefaultBoolean = true; |
| 31 | +} |
| 32 | + |
| 33 | +describe(ConfigurationFactory.name, () => { |
| 34 | + let module: TestingModule; |
| 35 | + let configFactory: ConfigurationFactory; |
| 36 | + let configService: DeepMocked<ConfigService>; |
| 37 | + |
| 38 | + beforeAll(async () => { |
| 39 | + module = await Test.createTestingModule({ |
| 40 | + providers: [ |
| 41 | + { |
| 42 | + provide: ConfigService, |
| 43 | + useValue: createMock<ConfigService>(), |
| 44 | + }, |
| 45 | + ], |
| 46 | + }).compile(); |
| 47 | + |
| 48 | + configService = module.get(ConfigService); |
| 49 | + configFactory = new ConfigurationFactory(configService); |
| 50 | + }); |
| 51 | + |
| 52 | + afterAll(async () => { |
| 53 | + await module.close(); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + jest.resetAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('loadAndValidateConfigs', () => { |
| 61 | + describe('when value is valid', () => { |
| 62 | + const setup = () => { |
| 63 | + jest.spyOn(configService, 'get').mockImplementation((key: string) => { |
| 64 | + if (key === 'TEST_VALUE_1') { |
| 65 | + return 'test'; |
| 66 | + } |
| 67 | + if (key === 'TEST_VALUE2') { |
| 68 | + return 'test2'; |
| 69 | + } |
| 70 | + if (key === 'valueWithoutDefault') { |
| 71 | + return 'testValueWithD'; |
| 72 | + } |
| 73 | + if (key === 'valueWithoutDefaultBoolean') { |
| 74 | + return 'true'; |
| 75 | + } |
| 76 | + |
| 77 | + return undefined; |
| 78 | + }); |
| 79 | + }; |
| 80 | + describe('when @ConfigProperty("TEST_VALUE_1") is provided by env variable', () => { |
| 81 | + it('should return the correct value', () => { |
| 82 | + setup(); |
| 83 | + const result = configFactory.loadAndValidateConfigs(TestConfig); |
| 84 | + |
| 85 | + expect(result.valueWithOtherEnvVar).toEqual('test'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('when @ConfigProperty("TEST_VALUE2") is provided by env variable', () => { |
| 90 | + it('should return the correct value', () => { |
| 91 | + setup(); |
| 92 | + const result = configFactory.loadAndValidateConfigs(TestConfig); |
| 93 | + |
| 94 | + expect(result.valueWithOtherEnvVarAndDefault).toEqual('test2'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('when @ConfigProperty() without default is provided by env variable', () => { |
| 99 | + it('should return the correct value', () => { |
| 100 | + setup(); |
| 101 | + const result = configFactory.loadAndValidateConfigs(TestConfig); |
| 102 | + |
| 103 | + expect(result.valueWithoutDefault).toEqual('testValueWithD'); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('when @ConfigProperty() without default is provided by env variable and transformed to boolean', () => { |
| 108 | + it('should return the correct value', () => { |
| 109 | + setup(); |
| 110 | + const result = configFactory.loadAndValidateConfigs(TestConfig); |
| 111 | + |
| 112 | + expect(result.valueWithoutDefaultBoolean).toEqual(true); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('when @ConfigProperty() with default is not provided by env variable', () => { |
| 117 | + it('should return the default value', () => { |
| 118 | + setup(); |
| 119 | + const result = configFactory.loadAndValidateConfigs(TestConfig); |
| 120 | + |
| 121 | + expect(result.valueWithDefaultBoolean).toEqual(true); |
| 122 | + }); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('when value is not valid', () => { |
| 127 | + const setup = () => { |
| 128 | + jest.spyOn(configService, 'get').mockImplementation((key: string) => { |
| 129 | + if (key === 'TEST_VALUE_1') { |
| 130 | + return 123; |
| 131 | + } |
| 132 | + if (key === 'TEST_VALUE2') { |
| 133 | + return 'test2'; |
| 134 | + } |
| 135 | + if (key === 'testValueWithD') { |
| 136 | + return 'testValueWithD'; |
| 137 | + } |
| 138 | + if (key === 'testValueWith') { |
| 139 | + return 'true'; |
| 140 | + } |
| 141 | + |
| 142 | + return undefined; |
| 143 | + }); |
| 144 | + }; |
| 145 | + it('should throw error', () => { |
| 146 | + setup(); |
| 147 | + expect(() => configFactory.loadAndValidateConfigs(TestConfig)).toThrow(/isString/); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('when The class is not decorated with @Configuration()', () => { |
| 152 | + class InvalidConfig { |
| 153 | + @IsString() |
| 154 | + @ConfigProperty('TEST_VALUE_1') |
| 155 | + public TEST_VALUE!: string; |
| 156 | + } |
| 157 | + |
| 158 | + it('should throw error', () => { |
| 159 | + expect(() => configFactory.loadAndValidateConfigs(InvalidConfig)).toThrow( |
| 160 | + `The class InvalidConfig is not decorated with @Configuration()`, |
| 161 | + ); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments