Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch EnvMissingError and send error to reporter when no default value is provided in dev #225

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,35 @@ export function getSanitizedEnv<S>(
const spec = castedSpecs[k]
const rawValue = readRawEnvValue(environment, k)

// If no value was given and default/devDefault were provided, return the appropriate default
// value without passing it through validation
if (rawValue === undefined) {
// Use devDefault values only if NODE_ENV was explicitly set, and isn't 'production'
const usingDevDefault =
rawNodeEnv && rawNodeEnv !== 'production' && spec.hasOwnProperty('devDefault')
if (usingDevDefault) {
cleanedEnv[k] = spec.devDefault

if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') {
throw new EnvMissingError(formatSpecDescription(spec))
try {
// If no value was given and default/devDefault were provided, return the appropriate default
// value without passing it through validation
if (rawValue === undefined) {
// Use devDefault values only if NODE_ENV was explicitly set, and isn't 'production'
const usingDevDefault =
rawNodeEnv && rawNodeEnv !== 'production' && spec.hasOwnProperty('devDefault')

if (usingDevDefault) {
cleanedEnv[k] = spec.devDefault

if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') {
throw new EnvMissingError(formatSpecDescription(spec))
}

continue
}

continue
}
if ('default' in spec) {
cleanedEnv[k] = spec.default
continue
}
}
if ('default' in spec) {
cleanedEnv[k] = spec.default
continue
}

try {
if (rawValue === undefined) {
// Throw error when no default value is provided
cleanedEnv[k] = undefined
throw new EnvMissingError(formatSpecDescription(spec))
} else {
cleanedEnv[k] = validateVar({ name: k as string, spec, rawValue })
}

cleanedEnv[k] = validateVar({ name: k as string, spec, rawValue })
} catch (err) {
if (options?.reporter === null) throw err
if (err instanceof Error) errors[k] = err
Expand Down
41 changes: 32 additions & 9 deletions tests/basics.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cleanEnv, str, num, testOnly } from '../src'
import { cleanEnv, str, num, testOnly, ReporterOptions } from '../src'
import { assertPassthrough } from './utils'
import { expectTypeOf } from 'expect-type'

Expand Down Expand Up @@ -226,23 +226,46 @@ describe('NODE_ENV built-in support', () => {

test('testOnly', () => {
const processEnv = process.env.NODE_ENV
const makeSpec = () => ({
FOO: str({ devDefault: testOnly('sup') }),

const reporter = jest.fn(({ errors = {} }: ReporterOptions<any>) => {
if (Object.keys(errors).length) {
throw new Error()
}
})

// Create an env spec that has our testOnly value applied as the devDefault,
// and then restore the original NODE_ENV
process.env.NODE_ENV = 'test'
const testSpec = makeSpec()
process.env.NODE_ENV = processEnv

const env = cleanEnv({ NODE_ENV: 'test' }, testSpec)
const env = cleanEnv(
{ NODE_ENV: 'test' },
{ FOO: str({ devDefault: testOnly('sup') }) },
{ reporter },
)
expect(env).toEqual({ FOO: 'sup' })
expect(reporter).toHaveBeenCalledTimes(1)
jest.clearAllMocks()

process.env.NODE_ENV = 'production'
expect(() => cleanEnv({ NODE_ENV: 'production' }, makeSpec(), makeSilent)).toThrow()
expect(() =>
cleanEnv(
{ NODE_ENV: 'production' },
{ FOO: str({ devDefault: testOnly('sup') }) },
{ reporter },
),
).toThrow()
expect(reporter).toHaveBeenCalledTimes(1)
jest.clearAllMocks()

process.env.NODE_ENV = 'development'
expect(() => cleanEnv({ NODE_ENV: 'development' }, makeSpec(), makeSilent)).toThrow()
expect(() =>
cleanEnv(
{ NODE_ENV: 'development' },
{ FOO: str({ devDefault: testOnly('sup') }) },
{ reporter },
),
).toThrow()
expect(reporter).toHaveBeenCalledTimes(1)
jest.clearAllMocks()

process.env.NODE_ENV = processEnv
})
Loading