|
| 1 | +import { loggerSchema } from '../joi'; |
| 2 | +import { |
| 3 | + JOI_LOGGER_COLORIZE, |
| 4 | + JOI_LOGGER_CUSTOM_LEVELS_ORDER, |
| 5 | + JOI_LOGGER_EXCLUDE, |
| 6 | + JOI_LOGGER_PRETTY, |
| 7 | + JOI_LOGGER_REDACT, |
| 8 | + LOGGER_LEVEL, |
| 9 | +} from '../logger.defaults'; |
| 10 | +import { LoggerLevel } from '../logger.interfaces'; |
| 11 | + |
| 12 | +describe('joi: loggerSchema (spec)', () => { |
| 13 | + // Joi type coercion by default |
| 14 | + // Fails asap |
| 15 | + [ |
| 16 | + { |
| 17 | + description: 'Should fail if LOGGER_LEVEL is unknown', |
| 18 | + env: { |
| 19 | + LOGGER_LEVEL: 'whatever', |
| 20 | + }, |
| 21 | + expected: `"LOGGER_LEVEL" must be one of [${Object.values(LoggerLevel).join(', ')}]`, |
| 22 | + }, |
| 23 | + { |
| 24 | + description: 'Should fail if LOGGER_CUSTOM_LEVELS_ORDER is not a valid bool', |
| 25 | + env: { |
| 26 | + LOGGER_CUSTOM_LEVELS_ORDER: 'whatever', |
| 27 | + }, |
| 28 | + expected: '"LOGGER_CUSTOM_LEVELS_ORDER" must be a boolean', |
| 29 | + }, |
| 30 | + { |
| 31 | + description: 'Should fail if LOGGER_PRETTY is not a valid bool', |
| 32 | + env: { |
| 33 | + LOGGER_PRETTY: 'whatever', |
| 34 | + }, |
| 35 | + expected: '"LOGGER_PRETTY" must be a boolean', |
| 36 | + }, |
| 37 | + { |
| 38 | + description: 'Should fail if LOGGER_COLORIZE is not a valid bool', |
| 39 | + env: { |
| 40 | + LOGGER_COLORIZE: 'whatever', |
| 41 | + }, |
| 42 | + expected: '"LOGGER_COLORIZE" must be a boolean', |
| 43 | + }, |
| 44 | + { |
| 45 | + description: 'Should fail if LOGGER_REDACT is invalid', |
| 46 | + env: { |
| 47 | + LOGGER_REDACT: 'whatever,', |
| 48 | + }, |
| 49 | + expected: undefined, |
| 50 | + }, |
| 51 | + { |
| 52 | + description: 'Should fail if LOGGER_EXCLUDE is invalid', |
| 53 | + env: { |
| 54 | + LOGGER_EXCLUDE: 'whatever,', |
| 55 | + }, |
| 56 | + expected: undefined, |
| 57 | + }, |
| 58 | + ].forEach(({ description, env, expected }) => |
| 59 | + it(`${description}`, () => { |
| 60 | + const r = loggerSchema.validate(env); |
| 61 | + |
| 62 | + expect(r.error?.details[0]?.message).toStrictEqual(expected); |
| 63 | + }), |
| 64 | + ); |
| 65 | + |
| 66 | + [ |
| 67 | + { |
| 68 | + description: 'Should NOT fail if NO env is provided', |
| 69 | + env: {}, |
| 70 | + expected: { |
| 71 | + LOGGER_LEVEL: LOGGER_LEVEL, |
| 72 | + LOGGER_CUSTOM_LEVELS_ORDER: JOI_LOGGER_CUSTOM_LEVELS_ORDER, |
| 73 | + LOGGER_PRETTY: JOI_LOGGER_PRETTY, |
| 74 | + LOGGER_COLORIZE: JOI_LOGGER_COLORIZE, |
| 75 | + LOGGER_REDACT: JOI_LOGGER_REDACT, |
| 76 | + LOGGER_EXCLUDE: JOI_LOGGER_EXCLUDE, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + description: 'Should NOT fail if LOGGER_CUSTOM_LEVELS_ORDER is a valid bool string', |
| 81 | + env: { |
| 82 | + LOGGER_CUSTOM_LEVELS_ORDER: 'true', |
| 83 | + }, |
| 84 | + expected: { |
| 85 | + LOGGER_LEVEL: LOGGER_LEVEL, |
| 86 | + LOGGER_CUSTOM_LEVELS_ORDER: true, |
| 87 | + LOGGER_PRETTY: JOI_LOGGER_PRETTY, |
| 88 | + LOGGER_COLORIZE: JOI_LOGGER_COLORIZE, |
| 89 | + LOGGER_REDACT: JOI_LOGGER_REDACT, |
| 90 | + LOGGER_EXCLUDE: JOI_LOGGER_EXCLUDE, |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + description: 'Should fail if LOGGER_PRETTY is a valid bool string', |
| 95 | + env: { |
| 96 | + LOGGER_PRETTY: 'false', |
| 97 | + }, |
| 98 | + expected: { |
| 99 | + LOGGER_LEVEL: LOGGER_LEVEL, |
| 100 | + LOGGER_CUSTOM_LEVELS_ORDER: JOI_LOGGER_CUSTOM_LEVELS_ORDER, |
| 101 | + LOGGER_PRETTY: false, |
| 102 | + LOGGER_COLORIZE: JOI_LOGGER_COLORIZE, |
| 103 | + LOGGER_REDACT: JOI_LOGGER_REDACT, |
| 104 | + LOGGER_EXCLUDE: JOI_LOGGER_EXCLUDE, |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + description: 'Should fail if LOGGER_COLORIZE is a valid bool string', |
| 109 | + env: { |
| 110 | + LOGGER_COLORIZE: 'false', |
| 111 | + }, |
| 112 | + expected: { |
| 113 | + LOGGER_LEVEL: LOGGER_LEVEL, |
| 114 | + LOGGER_CUSTOM_LEVELS_ORDER: JOI_LOGGER_CUSTOM_LEVELS_ORDER, |
| 115 | + LOGGER_PRETTY: JOI_LOGGER_PRETTY, |
| 116 | + LOGGER_COLORIZE: false, |
| 117 | + LOGGER_REDACT: JOI_LOGGER_REDACT, |
| 118 | + LOGGER_EXCLUDE: JOI_LOGGER_EXCLUDE, |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + description: 'Should NOT fail if LOGGER_REDACT is an empty string', |
| 123 | + env: { |
| 124 | + LOGGER_REDACT: '', |
| 125 | + }, |
| 126 | + expected: { |
| 127 | + LOGGER_LEVEL: LOGGER_LEVEL, |
| 128 | + LOGGER_CUSTOM_LEVELS_ORDER: JOI_LOGGER_CUSTOM_LEVELS_ORDER, |
| 129 | + LOGGER_PRETTY: JOI_LOGGER_PRETTY, |
| 130 | + LOGGER_COLORIZE: JOI_LOGGER_COLORIZE, |
| 131 | + LOGGER_REDACT: JOI_LOGGER_REDACT, |
| 132 | + LOGGER_EXCLUDE: JOI_LOGGER_EXCLUDE, |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + description: 'Should NOT fail if LOGGER_EXCLUDE is an empty string', |
| 137 | + env: { |
| 138 | + LOGGER_EXCLUDE: '', |
| 139 | + }, |
| 140 | + expected: { |
| 141 | + LOGGER_LEVEL: LOGGER_LEVEL, |
| 142 | + LOGGER_CUSTOM_LEVELS_ORDER: JOI_LOGGER_CUSTOM_LEVELS_ORDER, |
| 143 | + LOGGER_PRETTY: JOI_LOGGER_PRETTY, |
| 144 | + LOGGER_COLORIZE: JOI_LOGGER_COLORIZE, |
| 145 | + LOGGER_REDACT: JOI_LOGGER_REDACT, |
| 146 | + LOGGER_EXCLUDE: JOI_LOGGER_EXCLUDE, |
| 147 | + }, |
| 148 | + }, |
| 149 | + ].forEach(({ description, env, expected }) => |
| 150 | + it(`${description}`, () => { |
| 151 | + const r = loggerSchema.validate(env); |
| 152 | + |
| 153 | + expect(r.value).toStrictEqual(expected); |
| 154 | + expect(r.error?.details).toStrictEqual(undefined); |
| 155 | + }), |
| 156 | + ); |
| 157 | +}); |
0 commit comments