Skip to content

Commit 1c3e83b

Browse files
committed
add test for multiple scenarios
Signed-off-by: rockito10 <[email protected]>
1 parent 8d1893d commit 1c3e83b

File tree

2 files changed

+308
-4
lines changed

2 files changed

+308
-4
lines changed

config/core/helpers/url-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function _isEmail(input: string | undefined): boolean {
5151
}
5252

5353
function _isMultipleURL(input: string | undefined): boolean {
54-
return input.split(',').every((url) => _isURL(url.trim()) || _isLocalhost(url.trim()));
54+
return input?.split(',').every((url) => _isURL(url.trim()) || _isLocalhost(url.trim()));
5555
}
5656

5757
function _isPostgresURL(input: string | undefined): boolean {

config/test/config.spec.ts

Lines changed: 307 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,322 @@ dotenv.config({
55
path: './config/test/.env'
66
});
77

8-
test('Protocol Success', () => {
8+
// --- PROTOCOL ---
9+
10+
test('Protocol Success http', () => {
11+
const mockSchema = v.schema({
12+
PROTOCOL_TEST_1: v.str().protocol()
13+
});
14+
15+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
16+
});
17+
18+
test('Protocol Success https', () => {
919
const mockSchema = v.schema({
10-
API_GATEWAY_PROTOCOL: v.str().protocol()
20+
PROTOCOL_TEST_3: v.str().protocol()
1121
});
1222

1323
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
1424
});
1525

1626
test('Protocol Error', () => {
1727
const mockSchema = v.schema({
18-
API_GATEWAY_PROTOCOL_SECURE: v.str().protocol()
28+
PROTOCOL_TEST_2: v.str().protocol()
29+
});
30+
31+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
32+
});
33+
34+
// --- HOST ---
35+
36+
test('Host Success', () => {
37+
const mockSchema = v.schema({
38+
TEST_HOST_4: v.str().host()
39+
});
40+
41+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
42+
});
43+
44+
test('Host Error exceeds value of valid IP', () => {
45+
const mockSchema = v.schema({
46+
TEST_HOST_1: v.str().protocol()
47+
});
48+
49+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
50+
});
51+
52+
test('Host Error contains letters', () => {
53+
const mockSchema = v.schema({
54+
TEST_HOST_2: v.str().protocol()
55+
});
56+
57+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
58+
});
59+
60+
test('Host Error contains letters', () => {
61+
const mockSchema = v.schema({
62+
TEST_HOST_3: v.str().protocol()
63+
});
64+
65+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
66+
});
67+
68+
// --- URL ---
69+
70+
test('URL Success', () => {
71+
const mockSchema = v.schema({
72+
TEST_URL_1: v.str().url()
73+
});
74+
75+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
76+
});
77+
78+
test('URL Error invalid protocol', () => {
79+
const mockSchema = v.schema({
80+
TEST_URL_2: v.str().url()
81+
});
82+
83+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
84+
});
85+
86+
test('URL Success with Localhost', () => {
87+
const mockSchema = v.schema({
88+
TEST_URL_3: v.str().url()
89+
});
90+
91+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
92+
});
93+
94+
test('URL Error invalid format', () => {
95+
const mockSchema = v.schema({
96+
TEST_URL_4: v.str().url()
97+
});
98+
99+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
100+
});
101+
102+
test('URL Success without protocol', () => {
103+
const mockSchema = v.schema({
104+
TEST_URL_5: v.str().url()
105+
});
106+
107+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
108+
});
109+
110+
// --- MULTIPLE URL ---
111+
112+
test('Multiple URL error with no valid URL', () => {
113+
const mockSchema = v.schema({
114+
TEST_MULTIPLE_URL_1: v.str().multipleUrl()
115+
});
116+
117+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
118+
});
119+
120+
test('Multiple URL error with valid URLs, but one not valid', () => {
121+
const mockSchema = v.schema({
122+
TEST_MULTIPLE_URL_2: v.str().multipleUrl()
123+
});
124+
125+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
126+
});
127+
128+
test('Multiple URL with one URL', () => {
129+
const mockSchema = v.schema({
130+
TEST_MULTIPLE_URL_3: v.str().multipleUrl()
131+
});
132+
133+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
134+
});
135+
136+
test('Multiple URL with more than one URL', () => {
137+
const mockSchema = v.schema({
138+
TEST_MULTIPLE_URL_4: v.str().multipleUrl()
139+
});
140+
141+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
142+
});
143+
144+
// --- PORT ---
145+
146+
test('Port error with well-known port', () => {
147+
const mockSchema = v.schema({
148+
TEST_PORT_1: v.str().port()
149+
});
150+
151+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
152+
});
153+
154+
test('Port error with out-of-range port', () => {
155+
const mockSchema = v.schema({
156+
TEST_PORT_2: v.str().port()
157+
});
158+
159+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
160+
});
161+
162+
// --- ENDPOINT ---
163+
164+
test('Endpoint error, not an endpoint', () => {
165+
const mockSchema = v.schema({
166+
TEST_ENDPOINT_1: v.str().endpoint()
167+
});
168+
169+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
170+
});
171+
172+
test('Endpoint error, wrong format (has protocol)', () => {
173+
const mockSchema = v.schema({
174+
TEST_ENDPOINT_2: v.str().endpoint()
175+
});
176+
177+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
178+
});
179+
180+
test('Endpoint success with valid endpoint', () => {
181+
const mockSchema = v.schema({
182+
TEST_ENDPOINT_3: v.str().endpoint()
183+
});
184+
185+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
186+
});
187+
188+
// --- OPTIONAL ---
189+
190+
test('Optional success with no value', () => {
191+
const mockSchema = v.schema({
192+
TEST_OPTIONAL_1: v.str().optional()
193+
});
194+
195+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
196+
});
197+
198+
test('Optional success with value', () => {
199+
const mockSchema = v.schema({
200+
TEST_OPTIONAL_2: v.str().optional()
201+
});
202+
203+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
204+
});
205+
206+
// --- NOT EMPTY ---
207+
208+
test('Not empty failure with no value', () => {
209+
const mockSchema = v.schema({
210+
TEST_NOT_EMPTY_1: v.str().notEmpty()
211+
});
212+
213+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
214+
});
215+
216+
test('Not empty success with value', () => {
217+
const mockSchema = v.schema({
218+
TEST_NOT_EMPTY_2: v.str().notEmpty()
219+
});
220+
221+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
222+
});
223+
224+
// --- EMAIL ---
225+
226+
test('Email error with invalid mail format', () => {
227+
const mockSchema = v.schema({
228+
TEST_EMAIL_1: v.str().email()
19229
});
20230

21231
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
22232
});
233+
234+
test('Email error with invalid mail format (lacks ".something")', () => {
235+
const mockSchema = v.schema({
236+
TEST_EMAIL_2: v.str().email()
237+
});
238+
239+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
240+
});
241+
242+
test('Email error with invalid mail format (lacks "@")', () => {
243+
const mockSchema = v.schema({
244+
TEST_EMAIL_3: v.str().email()
245+
});
246+
247+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
248+
});
249+
250+
test('Email success with valid email', () => {
251+
const mockSchema = v.schema({
252+
TEST_EMAIL_4: v.str().email()
253+
});
254+
255+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
256+
});
257+
258+
// --- BOOLEAN ---
259+
260+
test('Boolean failure, is not true or false.', () => {
261+
const mockSchema = v.schema({
262+
TEST_BOOLEAN_1: v.str().boolean()
263+
});
264+
265+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
266+
});
267+
268+
test('Boolean failure, is not true or false.', () => {
269+
const mockSchema = v.schema({
270+
TEST_BOOLEAN_2: v.str().boolean()
271+
});
272+
273+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
274+
});
275+
276+
test('Boolean error, is uppercase true.', () => {
277+
const mockSchema = v.schema({
278+
TEST_BOOLEAN_3: v.str().boolean()
279+
});
280+
281+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
282+
});
283+
284+
test('Boolean success, is uppercase false.', () => {
285+
const mockSchema = v.schema({
286+
TEST_BOOLEAN_4: v.str().boolean()
287+
});
288+
289+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
290+
});
291+
292+
test('Boolean success, is true.', () => {
293+
const mockSchema = v.schema({
294+
TEST_BOOLEAN_5: v.str().boolean()
295+
});
296+
297+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
298+
});
299+
300+
test('Boolean success, is false.', () => {
301+
const mockSchema = v.schema({
302+
TEST_BOOLEAN_6: v.str().boolean()
303+
});
304+
305+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
306+
});
307+
308+
// --- NUMBER ---
309+
310+
test('Boolean error, is not a valid number.', () => {
311+
const mockSchema = v.schema({
312+
TEST_NUMBER_1: v.str().number()
313+
});
314+
315+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
316+
});
317+
318+
test('Boolean success, is a valid number.', () => {
319+
const mockSchema = v.schema({
320+
TEST_NUMBER_2: v.str().number()
321+
});
322+
323+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
324+
});
325+
326+
// TODO undefined cases

0 commit comments

Comments
 (0)