GithubHelp home page GithubHelp logo

mocked-env's Introduction

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 <[email protected]> © 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 <[email protected]>

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.

mocked-env's People

Contributors

renovate-bot avatar bahmutov avatar renovate[bot] avatar yinzara avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.