Skip to content

Commit

Permalink
Add testOnly util
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
SimenB authored and af committed Jun 16, 2017
1 parent 03801dd commit 36e81e5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
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')

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)
})

0 comments on commit 36e81e5

Please sign in to comment.