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

Add testOnly util #36

Merged
merged 1 commit into from
Jun 16, 2017
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ const validatedConfig = envalid.cleanEnv(
)
```

## Utils

### testOnly

A function called `testOnly` is exported. It returns its value if `NODE_ENV === 'test'`, otherwise it returns `undefined`.

## Motivation

Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const { EnvError, EnvMissingError, makeValidator,

const extend = (x = {}, y = {}) => Object.assign({}, x, y)

function testOnly(testDefault) {
return process.env.NODE_ENV === 'test'
? testDefault
: undefined
}

/**
* Validate a single env var, given a spec object
*
Expand Down Expand Up @@ -41,8 +47,8 @@ function extendWithDotEnv(inputEnv, dotEnvPath = '.env') {
// The react-native packager detects the require calls even if they
// are not on the top level, so we need to hide them by concatinating
// the module names.
const fs = require('f'+'s')
const dotenv = require('doten'+'v')
const fs = require('f' + 's')
const dotenv = require('doten' + 'v')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint --fix did this (was just warning, not error)


let dotEnvBuffer = null
try {
Expand Down Expand Up @@ -121,5 +127,5 @@ function cleanEnv(inputEnv, specs = {}, options = {}) {
}

module.exports = {
cleanEnv, makeValidator, bool, num, str, json, url, email, EnvError, EnvMissingError
cleanEnv, makeValidator, bool, num, str, json, url, email, EnvError, EnvMissingError, testOnly
}
21 changes: 20 additions & 1 deletion tests/test_basics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { createGroup, assert } = require('painless')
const { cleanEnv, EnvError, EnvMissingError, str, num } = require('..')
const { cleanEnv, EnvError, EnvMissingError, str, num, testOnly } = require('..')
const { assertPassthrough } = require('./utils')
const test = createGroup()
const makeSilent = { reporter: null }
Expand Down Expand Up @@ -146,3 +146,22 @@ test('NODE_ENV built-in support', () => {
assert.strictEqual(cleanEnv({}, customSpec).isProduction, false)
assert.strictEqual(cleanEnv({}, customSpec).isDev, false)
})

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

process.env.NODE_ENV = processEnv
const env = cleanEnv({ NODE_ENV: 'test' }, spec)
assert.deepEqual(env, { NODE_ENV: 'test', FOO: 'sup' })

spec = {
FOO: str({ devDefault: testOnly('sup') })
}

assert.throws(() => cleanEnv({ NODE_ENV: 'production' }, spec, makeSilent), EnvMissingError)
assert.throws(() => cleanEnv({ NODE_ENV: 'development' }, spec, makeSilent), EnvMissingError)
})