Skip to content

Commit 16a8773

Browse files
committed
Remove now-redundant testOnly util
1 parent ade4589 commit 16a8773

5 files changed

Lines changed: 13 additions & 105 deletions

File tree

README.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -221,32 +221,6 @@ argument required in the third position:
221221
middleware (see src/middleware.ts), which you can mix and match with your own
222222
custom logic to get the behavior you desire.
223223

224-
## Utils
225-
226-
### testOnly
227-
228-
The `testOnly` helper function is available for setting a default value for an env var only when `NODE_ENV=test`. It is used by wrapping a `devDefault` value:
229-
230-
```js
231-
const env = cleanEnv(process.env, {
232-
SOME_VAR: envalid.str({ devDefault: testOnly('myTestValue') }),
233-
})
234-
```
235-
236-
Because `testOnly` is applied via `devDefault`, you cannot use it to express both a development default _and_ a separate test default. For that, prefer the `testDefault` spec attribute, which can be combined freely with `default` and `devDefault`:
237-
238-
```js
239-
const env = cleanEnv(process.env, {
240-
SOME_VAR: envalid.str({
241-
default: 'productionValue',
242-
devDefault: 'devValue',
243-
testDefault: 'myTestValue',
244-
}),
245-
})
246-
```
247-
248-
For more context see [this issue](https://github.com/af/envalid/issues/32).
249-
250224
## FAQ
251225

252226
### Can I call `structuredClone()` on Envalid's validated output?

src/core.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { EnvError, EnvMissingError } from './errors'
22
import type { CleanOptions, SpecsOutput, Spec, ValidatorSpec } from './types'
33
import { defaultReporter } from './reporter'
44

5-
export const testOnlySymbol = Symbol('envalid - test only')
6-
75
/**
86
* Validate a single env var, given a spec object
97
*
@@ -46,8 +44,6 @@ const readRawEnvValue = <T>(env: unknown, k: keyof T | 'NODE_ENV'): string | T[k
4644
return (env as any)[k]
4745
}
4846

49-
const isTestOnlySymbol = (value: any): value is symbol => value === testOnlySymbol
50-
5147
/**
5248
* Perform the central validation/sanitization logic on the full environment object
5349
*/
@@ -82,11 +78,6 @@ export function getSanitizedEnv<S>(
8278

8379
if (usingDevDefault) {
8480
cleanedEnv[k] = spec.devDefault
85-
86-
if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') {
87-
throw new EnvMissingError(formatSpecDescription(spec))
88-
}
89-
9081
continue
9182
}
9283

@@ -107,7 +98,7 @@ export function getSanitizedEnv<S>(
10798
}
10899
}
109100

110-
// This block is for supporting requiredWhen. If that field was provided for a var's spec and
101+
// This block is for supporting requiredWhen. If that field was provided for a var's spec and
111102
// its condition evaluates to a truthy value, ensure that env var is present.
112103
for (const k of varKeys) {
113104
if (errors[k] == undefined) {

src/envalid.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CleanedEnv, CleanOptions } from './types'
2-
import { getSanitizedEnv, testOnlySymbol } from './core'
2+
import { getSanitizedEnv } from './core'
33
import { applyDefaultMiddleware } from './middleware'
44

55
/**
@@ -38,12 +38,3 @@ export function customCleanEnv<S, MW>(
3838
const cleaned = getSanitizedEnv(environment, specs, options)
3939
return Object.freeze(applyMiddleware(cleaned as CleanedEnv<S>, environment))
4040
}
41-
42-
/**
43-
* Utility function for providing default values only when NODE_ENV=test
44-
*
45-
* For more context, see https://github.com/af/envalid/issues/32
46-
*/
47-
export const testOnly = <T>(defaultValueForTests: T) => {
48-
return process.env.NODE_ENV === 'test' ? defaultValueForTests : (testOnlySymbol as unknown as T) // T is not strictly correct, but prevents type errors during usage
49-
}

src/types.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ export interface Spec<T> {
2626
devDefault?: NonNullable<T> | undefined
2727
/**
2828
* A fallback value to use only when NODE_ENV is 'test'. Takes priority over `devDefault` and `default`.
29-
* Unlike the `testOnly()` helper (which wraps a `devDefault` value), `testDefault` can be combined
30-
* with both `default` and `devDefault` to specify distinct values for production, development, and test.
3129
*/
3230
testDefault?: NonNullable<T> | undefined
3331

@@ -125,16 +123,16 @@ export type SpecsOutput<S> = {
125123

126124
export type CleanedEnv<S> =
127125
S extends Record<string, ValidatorSpec<unknown>>
128-
? Readonly<
129-
{
130-
[K in keyof S]: S[K] extends OptionalValidatorSpec<infer U>
131-
? U | undefined
132-
: S[K] extends RequiredValidatorSpec<infer U>
133-
? U
134-
: never
135-
} & CleanedEnvAccessors
136-
>
137-
: never
126+
? Readonly<
127+
{
128+
[K in keyof S]: S[K] extends OptionalValidatorSpec<infer U>
129+
? U | undefined
130+
: S[K] extends RequiredValidatorSpec<infer U>
131+
? U
132+
: never
133+
} & CleanedEnvAccessors
134+
>
135+
: never
138136

139137
export interface CleanedEnvAccessors {
140138
/** true if NODE_ENV === 'development' */

tests/basics.test.ts

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, test, expect, vi } from 'vitest'
2-
import { cleanEnv, str, num, testOnly, type ReporterOptions } from '../src'
2+
import { cleanEnv, str, num, type ReporterOptions } from '../src'
33
import { assertPassthrough } from './utils'
44
import { expectTypeOf } from 'expect-type'
55

@@ -297,49 +297,3 @@ describe('NODE_ENV built-in support', () => {
297297
expect(cleanEnv({}, customSpec).isDevelopment).toEqual(false)
298298
})
299299
})
300-
301-
test('testOnly', () => {
302-
const processEnv = process.env.NODE_ENV
303-
304-
const reporter = vi.fn(({ errors = {} }: ReporterOptions<any>) => {
305-
if (Object.keys(errors).length) {
306-
throw new Error()
307-
}
308-
})
309-
310-
// Create an env spec that has our testOnly value applied as the devDefault,
311-
// and then restore the original NODE_ENV
312-
process.env.NODE_ENV = 'test'
313-
const env = cleanEnv(
314-
{ NODE_ENV: 'test' },
315-
{ FOO: str({ devDefault: testOnly('sup') }) },
316-
{ reporter },
317-
)
318-
expect(env).toEqual({ FOO: 'sup' })
319-
expect(reporter).toHaveBeenCalledTimes(1)
320-
vi.clearAllMocks()
321-
322-
process.env.NODE_ENV = 'production'
323-
expect(() =>
324-
cleanEnv(
325-
{ NODE_ENV: 'production' },
326-
{ FOO: str({ devDefault: testOnly('sup') }) },
327-
{ reporter },
328-
),
329-
).toThrow()
330-
expect(reporter).toHaveBeenCalledTimes(1)
331-
vi.clearAllMocks()
332-
333-
process.env.NODE_ENV = 'development'
334-
expect(() =>
335-
cleanEnv(
336-
{ NODE_ENV: 'development' },
337-
{ FOO: str({ devDefault: testOnly('sup') }) },
338-
{ reporter },
339-
),
340-
).toThrow()
341-
expect(reporter).toHaveBeenCalledTimes(1)
342-
vi.clearAllMocks()
343-
344-
process.env.NODE_ENV = processEnv
345-
})

0 commit comments

Comments
 (0)