Skip to content

Commit 4d2e92d

Browse files
committed
Finish tests, prepare for merge
Signed-off-by: rockito10 <[email protected]>
1 parent 1c3e83b commit 4d2e92d

File tree

3 files changed

+153
-17
lines changed

3 files changed

+153
-17
lines changed

config/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,6 @@ if (!parsedEnv.success) {
164164
throw parsedEnv.error.flatten();
165165
}
166166

167-
const { data } = parsedEnv;
167+
const { data: env } = parsedEnv;
168168

169-
export { data };
169+
export { env };

config/test/.env.test.sample

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
PROTOCOL_TEST_1=hello # FAIL
2+
PROTOCOL_TEST_2=http # PASS
3+
PROTOCOL_TEST_3=https # PASS
4+
5+
TEST_HOST_1=193.168.52.252252252 # FAIL
6+
TEST_HOST_2=193.168.52.A # FAIL
7+
TEST_HOST_3=193.168.52 # FAIL
8+
TEST_HOST_4=1.1.1.1 # PASS
9+
10+
TEST_URL_1 =hello://api.credebl.id # FAIL
11+
TEST_URL_2 =https://api.credebl.id # PASS
12+
TEST_URL_3 =http://localhost:5000 # PASS
13+
TEST_URL_4 =https//api.credebl.id # PASS
14+
TEST_URL_5 =api.credebl.id # PASS
15+
16+
TEST_PORT_1=1000 # FAIL
17+
TEST_PORT_2=99999 # FAIL
18+
TEST_PORT_3=3000 # PASS
19+
20+
TEST_OPTIONAL_1= # FAIL
21+
TEST_OPTIONAL_2=HELLO WORLD # PASS
22+
23+
TEST_NOT_EMPTY_1= # FAIL
24+
TEST_NOT_EMPTY_2=HELLO WORLD # PASS
25+
26+
TEST_EMAIL_1=HELLO WORLD # FAIL
27+
TEST_EMAIL_2=name@gmail # FAIL
28+
TEST_EMAIL_3=namegmail.com # FAIL
29+
30+
31+
TEST_BOOLEAN_1=1000 # FAIL
32+
TEST_BOOLEAN_2=BOOL # FAIL
33+
TEST_BOOLEAN_3=FALSE # FAIL
34+
TEST_BOOLEAN_4=TRUE # FAIL
35+
TEST_BOOLEAN_5=true # PASS
36+
TEST_BOOLEAN_6=false # PASS
37+
38+
TEST_NUMBER_1=HELLO WORLD # FAIL
39+
TEST_NUMBER_2=1000 # PASS
40+
41+
TEST_ENDPOINT_1=HELLO WORLD # FAIL
42+
TEST_ENDPOINT_2=http://localhost:3000 # FAIL
43+
TEST_ENDPOINT_3=localhost:3000 # PASS
44+
45+
TEST_MULTIPLE_URL_1=HELLO WORLD # FAIL
46+
TEST_MULTIPLE_URL_2=http://localhost:3000, HELLO WORLD, www.google.com # FAIL
47+
TEST_MULTIPLE_URL_3=http://localhost:3000 # PASS
48+
TEST_MULTIPLE_URL_4=http://localhost:3000, http://localhost:4000, www.google.com # PASS
49+
50+
TEST_POSTGRES_URL_1=helloworld://postgres:postgres@localhost:5432/credebl # FAIL (must start with postgresql://)
51+
TEST_POSTGRES_URL_2=postgresql://postgres:postgreslocalhost:99999/credebl # FAIL (missing @)
52+
TEST_POSTGRES_URL_3=postgresql://postgres:postgres@helloworld:5432/credebl # FAIL (helloworld is not a valid host)
53+
TEST_POSTGRES_URL_4=postgresql://postgres:postgres@localhost:5432/credebl # PASS
54+
55+
TEST_LOCALHOST_1=localhost # FAIL
56+
TEST_LOCALHOST_2=localhost:3000 # PASS
57+
TEST_LOCALHOST_3=http://localhost:3000 # PASS
58+
59+
TEST_START_WITH_1=HELLO # FAIL
60+
TEST_START_WITH_2=WORLD ! ! # PASS
61+

config/test/config.spec.ts

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ import { v } from '../core';
22

33
import * as dotenv from 'dotenv';
44
dotenv.config({
5-
path: './config/test/.env'
5+
path: './config/test/.env.test.sample'
66
});
77

88
// --- PROTOCOL ---
99

10-
test('Protocol Success http', () => {
10+
test('Protocol Error', () => {
1111
const mockSchema = v.schema({
1212
PROTOCOL_TEST_1: v.str().protocol()
1313
});
1414

15-
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
15+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
1616
});
1717

18-
test('Protocol Success https', () => {
18+
test('Protocol Success http', () => {
1919
const mockSchema = v.schema({
20-
PROTOCOL_TEST_3: v.str().protocol()
20+
PROTOCOL_TEST_2: v.str().protocol()
2121
});
2222

2323
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
2424
});
2525

26-
test('Protocol Error', () => {
26+
test('Protocol Success https', () => {
2727
const mockSchema = v.schema({
28-
PROTOCOL_TEST_2: v.str().protocol()
28+
PROTOCOL_TEST_3: v.str().protocol()
2929
});
3030

31-
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
31+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
3232
});
3333

3434
// --- HOST ---
@@ -65,24 +65,49 @@ test('Host Error contains letters', () => {
6565
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
6666
});
6767

68-
// --- URL ---
68+
// --- LOCALHOST ---
6969

70-
test('URL Success', () => {
70+
test('LOCALHOST error, doesnt have port number.', () => {
7171
const mockSchema = v.schema({
72-
TEST_URL_1: v.str().url()
72+
TEST_LOCALHOST_1: v.str().localhost()
73+
});
74+
75+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
76+
});
77+
78+
test('LOCALHOST success, doesnt have protocol.', () => {
79+
const mockSchema = v.schema({
80+
TEST_LOCALHOST_2: v.str().localhost()
81+
});
82+
83+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
84+
});
85+
86+
test('LOCALHOST success, with protocol.', () => {
87+
const mockSchema = v.schema({
88+
TEST_LOCALHOST_3: v.str().localhost()
7389
});
7490

7591
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
7692
});
7793

94+
// --- URL ---
7895
test('URL Error invalid protocol', () => {
7996
const mockSchema = v.schema({
80-
TEST_URL_2: v.str().url()
97+
TEST_URL_1: v.str().url()
8198
});
8299

83100
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
84101
});
85102

103+
test('URL Success', () => {
104+
const mockSchema = v.schema({
105+
TEST_URL_2: v.str().url()
106+
});
107+
108+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
109+
});
110+
86111
test('URL Success with Localhost', () => {
87112
const mockSchema = v.schema({
88113
TEST_URL_3: v.str().url()
@@ -307,20 +332,70 @@ test('Boolean success, is false.', () => {
307332

308333
// --- NUMBER ---
309334

310-
test('Boolean error, is not a valid number.', () => {
335+
test('Number error, is not a valid number.', () => {
311336
const mockSchema = v.schema({
312337
TEST_NUMBER_1: v.str().number()
313338
});
314339

315340
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
316341
});
317342

318-
test('Boolean success, is a valid number.', () => {
343+
test('Number success, is a valid number.', () => {
319344
const mockSchema = v.schema({
320345
TEST_NUMBER_2: v.str().number()
321346
});
322347

323348
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
324349
});
325350

326-
// TODO undefined cases
351+
// --- POSTGRESURL ---
352+
353+
test('POSTGRESURL error, doesnt start with postgresql://.', () => {
354+
const mockSchema = v.schema({
355+
TEST_POSTGRES_URL_1: v.str().postgresUrl()
356+
});
357+
358+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
359+
});
360+
361+
test('POSTGRESURL error, wrong format, doesnt contain @.', () => {
362+
const mockSchema = v.schema({
363+
TEST_POSTGRES_URL_2: v.str().postgresUrl()
364+
});
365+
366+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
367+
});
368+
369+
test('POSTGRESURL error, invalid host.', () => {
370+
const mockSchema = v.schema({
371+
TEST_POSTGRES_URL_3: v.str().postgresUrl()
372+
});
373+
374+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
375+
});
376+
377+
test('POSTGRESURL success, valid postgres url.', () => {
378+
const mockSchema = v.schema({
379+
TEST_POSTGRES_URL_4: v.str().postgresUrl()
380+
});
381+
382+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
383+
});
384+
385+
// --- STARTS WITH ---
386+
387+
test('Startswith error, doesnt starts with given value.', () => {
388+
const mockSchema = v.schema({
389+
TEST_START_WITH_1: v.str().startsWith('AVALUE')
390+
});
391+
392+
expect(mockSchema.safeParse(process.env).success).toBeFalsy();
393+
});
394+
395+
test('Startswith success, starts with given value.', () => {
396+
const mockSchema = v.schema({
397+
TEST_START_WITH_2: v.str().startsWith('WORLD')
398+
});
399+
400+
expect(mockSchema.safeParse(process.env).success).toBeTruthy();
401+
});

0 commit comments

Comments
 (0)