1.3.5 • Published 3 years ago

mocked-env v1.3.5

Weekly downloads
47,867
License
MIT
Repository
github
Last release
3 years ago

mocked-env

Easy way to mock process.env during BDD testing

NPM

Build status semantic-release standard renovate-app badge

Read Mocking process.env blog post.

Install

Requires Node version 6 or above.

npm install --save-dev mocked-env

Use

Change values

const mockedEnv = require('mocked-env')
// before the test
let restore = mockedEnv({
  FOO: 'fake foo value',
  BAR: '42',
})
// use process.env.FOO and process.env.BAR during testing
// process.env.FOO = "fake foo value"
// process.env.BAR = "42"
// after the test
restore()
// any previous values of FOO and BAR restored

Delete values

If you want to temporarily delete environment variable, pass undefined value

let restore = mockedEnv({
  FOO: 'fake foo value',
  PWD: undefined, // will be deleted from process.env
})

Clear entire object

If you want to remove all existing properties and just set some, use option clear: true

let restore = mockedEnv(
  {
    FOO: 'foo',
    BAR: 'bar',
  },
  { clear: true }
)
// process.env = {FOO: 'foo', BAR: 'bar'}

Again, calling restore() will restore the original full process.env object.

Restore environment without clearing

If you want to maintain the current environment and restore it to the original state after restore() is called, pass the 'restore' option.

let restore = mockedEnv(
  {
    FOO: 'foo',
    BAR: 'bar',
  },
  { restore: true }
)
// process.env = {...process.env, FOO: 'foo', BAR: 'bar'}

These options are mutually exclusive and specifying them both will result in an error.

Options as first argument

The options array can be passed as the first argument to the mockedEnv function as long as it contains either a 'restore' or 'clear' key, not both.

let restore = mockedEnv(
  { clear: true }
)
// process.env = {}

⚠️ Note: process.env values should always be strings (Stackoverflow), any call to mockedEnv that attempts to use values other than strings (or undefined to signify that a property should be deleted) will raise an error.

Example

const mockedEnv = require('mocked-env')
describe('changes variables', () => {
  let restore // to restore old values

  beforeEach(() => {
    restore = mockedEnv({
      PWD: '/foo/bar',
      USER: undefined, // will be deleted from process.env
    })
  })

  it('has changed process.env', () => {
    // process.env.PWD = '/foo/bar'
  })

  afterEach(() => restore())
})

See src/example-spec.js

You can also nest process.env reset and setting individual values in inner suites, see src/nested-spec.js. Great for tests that need to control process.env and would like to reset the entire thing, but then mock individual properties.

Debugging

Run with DEBUG=mocked-env environment variable.

More information

This package was inspired by burl/mock-env

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2018

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2018 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.