@@ -4,6 +4,7 @@ import { EnumVar } from '../src'
4
4
import { optional , TypedEnv } from '../src'
5
5
import { IntVar } from '../src'
6
6
import { StringVar } from '../src'
7
+ import { nullable } from '../src/env'
7
8
8
9
describe ( 'TypedEnv' , ( ) => {
9
10
test ( 'basic parsing works' , async ( ) => {
@@ -110,15 +111,15 @@ describe('TypedEnv', () => {
110
111
) . toThrow ( 'too small' )
111
112
} )
112
113
113
- test ( 'handles optional variables' , async ( ) => {
114
+ test ( 'handles nullable variables' , async ( ) => {
114
115
const rawEnv = {
115
116
A : 'A' ,
116
117
}
117
118
expect (
118
119
TypedEnv (
119
120
{
120
- A : optional ( StringVar ( ) ) ,
121
- B : optional ( StringVar ( ) ) ,
121
+ A : nullable ( StringVar ( ) ) ,
122
+ B : nullable ( StringVar ( ) ) ,
122
123
} ,
123
124
rawEnv
124
125
)
@@ -137,9 +138,9 @@ describe('TypedEnv', () => {
137
138
delete process . env . SOME_RANDOM_VAR_NAME
138
139
} )
139
140
140
- test ( "Validators don't fire for nulls on optionals " , async ( ) => {
141
+ test ( "Validators don't fire for nulls on nullables " , async ( ) => {
141
142
const schema = {
142
- A : optional (
143
+ A : nullable (
143
144
IntVar ( {
144
145
validator ( ) {
145
146
throw new Error ( 'nope' )
@@ -152,4 +153,26 @@ describe('TypedEnv', () => {
152
153
expect ( ( ) => TypedEnv ( schema , rawEnv ) ) . toThrow ( 'nope' )
153
154
expect ( TypedEnv ( schema , emptyEnv ) . A ) . toBeNull ( )
154
155
} )
156
+
157
+ test ( 'Optionals return undefined instead of null' , async ( ) => {
158
+ const schema = {
159
+ A : optional ( StringVar ( ) ) ,
160
+ B : optional (
161
+ StringVar ( {
162
+ validator ( ) {
163
+ throw new Error ( 'nope' )
164
+ } ,
165
+ } )
166
+ ) ,
167
+ C : optional (
168
+ StringVar ( {
169
+ validator ( ) {
170
+ return true
171
+ } ,
172
+ } )
173
+ ) ,
174
+ }
175
+ const emptyEnv = { C : 'foo' }
176
+ expect ( TypedEnv ( schema , emptyEnv ) . A ) . toBeUndefined ( )
177
+ } )
155
178
} )
0 commit comments