Giter Club home page Giter Club logo

Comments (9)

gunar avatar gunar commented on May 24, 2024 1

@evanshortiss My unit tests require files that require the config.js file, which forces me to have the env vars set in order for tests to pass.

Unit tests don't depend on any environment variable (e.g. AUTH_TOKEN), but I still want to use env-var ❤️


Code example

// src/config.js
import * as env from 'env-var'

export const AUTH_TOKEN = env.get('AUTH_TOKEN').required().asString()
// src/businessLogic.js
import * from src/config

// ... business logic code ...
// test/businessLogic.spec.js

import * from src/businessLogic

// ... tests ...

from env-var.

gunar avatar gunar commented on May 24, 2024 1
const SECRET = env.get('SECRET', 'bad-secret').required(NODE_ENV === 'production').asString()

Very smart.

from env-var.

evanshortiss avatar evanshortiss commented on May 24, 2024

@gunar it sounds like you'd like to be able to have the required() function behave differently based on a specific condition - in your case that condition is the value NODE_ENV?

Would you mind providing a code example of how you would like to see this feature work?

from env-var.

evanshortiss avatar evanshortiss commented on May 24, 2024

I understand now. I don't think NODE_ENV=test is standard so I would be very cautious adding it to env-var since people will not expect that behaviour, right?

I have some thoughts:

  1. Use stubs or mocks in your tests. This is probably the best choice in general.
  2. Modify the config.js to change behaviour in test mode.
  3. Add a new feature to env-var.
  4. Use dependency injection - I list this since it's an option, but might need you to restructure modules

Option 1

Using mocks in your unit tests will allow you to stub/mock out config.js entirely. This is a good approach since you can adjust it for each test case if necessary. I'm not sure what text framework you use, but Jest supports class mocks and has a host of other mocking features. If not using jest, then maybe use proxyquire with mocha or another test runner?

Option 2

You could use fake values if in test mode:

import * as env from 'env-var'

const isTest = env.get('NODE_ENV').asString() === 'test'

const AUTH_TOKEN = isTest ? 'FAKE_VALUE' : env.get('AUTH_TOKEN').required().asString()

Option 3

Add a feature like this in env-var where required accepts an optional parameter function to customise the behaviour.

import * as env from 'env-var'

const isRequired = () => env.get('NODE_ENV').asString() !== 'test'

// required only throw if NODE_ENV is != "test"
export const AUTH_TOKEN = env.get('AUTH_TOKEN').required(isRequired).asString()

from env-var.

evanshortiss avatar evanshortiss commented on May 24, 2024

option 4

// src/businessLogic

// We do not require config.js here, instead we have it passed by the callee
export default function (config) {
  // use config.AUTH_TOKEN
}

Then in the test:

// test/businessLogic.spec.js

import businessLogic from src/businessLogic

const bl = bl({ AUTH_TOKEN: 'fake token for testing' })

Or in real code:

import businessLogic from src/businessLogic
import * from src/config as config

const bl = bl(config)

from env-var.

evanshortiss avatar evanshortiss commented on May 24, 2024

@gunar let me know your thoughts

from env-var.

evanshortiss avatar evanshortiss commented on May 24, 2024

@gunar closing this, but if you have any further input feel free to reopen.

from env-var.

evanshortiss avatar evanshortiss commented on May 24, 2024

@gunar, check out the new "required()" function behavior. It can support this now.

from env-var.

gunar avatar gunar commented on May 24, 2024

Thanks @evanshortiss !

from env-var.

Related Issues (20)

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.