Code Monkey home page Code Monkey logo

Comments (5)

markstos avatar markstos commented on June 2, 2024 1

@KunalBurangi You previously asked the same question in #651. Then I suggested you search previous issues the the related issues. Here you are asking the same question again with no hint that you've checked for previous issue discussion about this.

Also, node-config has opened Github discussions as another way to get support.

node-config is immutable by design once the configuration is set. As @jantaylor pointed out, there are some solutions for the testing case.

from node-config.

hacknlove avatar hacknlove commented on June 2, 2024 1
const config = require('config');

jest.mock('config', () => {
    const config = jest.requireActual('config');

    const configMock = {

    }

    return new Proxy(config, {
        get: (target, prop) => {
            if (prop === 'get') {
                return key => configMock.hasOwnProperty(key) ? configMock[key] : target[key];
            }

            if (prop === '__reset__') {
                return () => {
                    Object.keys(configMock).forEach(key => delete configMock[key]);
                }
            }

            return configMock[prop] ?? target[prop];
        },
        set: (target, prop, value) => {
            if (prop === 'get') {
                return false;
            }

            configMock[prop] = value;
            return true;
        }
    })
});


beforeEach(() => {
    config.__reset__();
});

it('allows to change the config for testing purposes', () => {
    config.foo = "bar";
    
    expect(config.foo).toBe("bar");
    
    config.foo = "buz";
    
    expect(config.foo).toBe("buz");
});

from node-config.

jantaylor avatar jantaylor commented on June 2, 2024

You have a couple options. You could pull the initial timeout from config into a variable. Then update your variable through the tests

let STATIC_TIMEOUT = Number(config.get('timeout'))

// In tests
STATIC_TIMEOUT = 30

// later tests
STATIC_TIMEOUT = 600

You could use Jest/Mocha mocks

You could convert your class/module that handles the timeout to take an input with a variable that changes and instantiate the new class with the changed timeouts OR allow updating the timeout with a method.

// Initial class
const timeoutClass = new Timeout(Number(config.get('timeout')))

// new class with different timeout
const shortTimeoutClass = new Timeout(30)

// Or use a method
timeoutClass.updateTimeout(30)

from node-config.

jantaylor avatar jantaylor commented on June 2, 2024

I said all that, but then I found this wiki doc for you @KunalBurangi https://github.com/node-config/node-config/wiki/Altering-configuration-values-for-testing-at-runtime

const mockRequire = require("mock-require");
const importFresh = require("import-fresh");

// set up config overrides so that calls to http services will fail fast
process.env.NODE_CONFIG = JSON.stringify({
  httpServices: {retryInterval: 1, retries: 1} 
});

const testConfig = importFresh("config");
expect(
  testConfig.get("httpServices.retryInterval"),
  "config value not set to 1"
).to.equal(1);
mockRequire("config", testConfig);

// set up and run tests 

from node-config.

KunalBurangi avatar KunalBurangi commented on June 2, 2024

@jantaylor - So basically the only option is to update the variable before you initialize the config right...
no other option as the Config object is non-extensible so no test runner like moha or Sinon can't stub it...

from node-config.

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.