diff --git a/README.md b/README.md index af28558..8ce6eac 100644 --- a/README.md +++ b/README.md @@ -89,3 +89,7 @@ By default, the object returned by configuer is frozen. Since it is also flat, the object is completely immutable. The `mutable` option makes configeur return an unfrozen object. This is not recommended in general, but may be useful for testing purposes. + +### `env` + +If you prefer to pass an environment object in manually, use this field. diff --git a/index.d.ts b/index.d.ts index 6cae534..b4213ed 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,6 +13,7 @@ type Parser = [string, (string) => any]; interface Options { parsers?: Parser[], mutable?: Boolean + env?: Object } export default function configeur(environment: Environment, options?: Options): Object; diff --git a/index.js b/index.js index 4bcce55..92d9dc1 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const processConfig = require('./lib/processConfig'); const makeParsers = require('./lib/makeParsers'); module.exports = function configeur(schema, options = {}) { - return processConfig(schema, process.env, makeParsers(options.parsers), options.mutable); + return processConfig(schema, options.env || process.env, makeParsers(options.parsers), options.mutable); }; module.exports.default = module.exports; diff --git a/test/index.test.js b/test/index.test.js index 3484af0..65f8b18 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -48,8 +48,25 @@ describe('configeur', () => { }); it('passes the schema, the environment, and the parsers to processConfig', () => { - configeur('fakeSchema', { parsers: 'fake-custom-parsers', mutable: 'is-mutable' }); + configeur('fakeSchema', { + parsers: 'fake-custom-parsers', + mutable: 'is-mutable' + }); + + assert.deepEqual(processConfigStub.args, [ + ['fakeSchema', { AN_ARG: 'argValue' }, 'fakeParsers', 'is-mutable'] + ]); + }); + + it('uses the optional env field to override process.env', () => { + configeur('fakeSchema', { + parsers: 'fake-custom-parsers', + mutable: 'is-mutable', + env: { A_DIFFERENT_ARG: 'differentValue' } + }); - assert.deepEqual(processConfigStub.args, [['fakeSchema', { AN_ARG: 'argValue' }, 'fakeParsers', 'is-mutable']]); + assert.deepEqual(processConfigStub.args, [ + ['fakeSchema', { A_DIFFERENT_ARG: 'differentValue' }, 'fakeParsers', 'is-mutable'] + ]); }); });