【问题标题】:How to mutate node-config in memory如何在内存中改变节点配置
【发布时间】:2018-12-06 17:52:00
【问题描述】:

我正在尝试更改内存中配置的值以进行测试,我尝试在应用程序的多个位置以及通过命令行和我的.env 文件添加process.env.ALLOW_CONFIG_MUTATIONS=true。

config.util.getEnv('ALLOW_CONFIG_MUTATION') 方法总是返回 undefined。

根据我在网上看到的示例,我还尝试使用importFresh 和MockRequest,这两种方法都不允许我更改内存中的配置,然后稍后重置该值。

有人对此有任何想法吗?

更新:这是我正在尝试完成的示例

const config = require (config);
const app = new App(config)

it(`does a thing with base config`, () => {  ...  }

it('does a thing with modified config, () => {  
    // here i would need to modify my config value and
    // have it change the original config that's currently in 
    // application memory

    config = newConfig  

    expect(config.get('newValues')).to.equal(true)
}

谢谢!

【问题讨论】:

    标签: javascript node.js node-modules node-config


    【解决方案1】:

    如果它是我使用的相同配置模块(我想我是),那么添加一个 custom-environment-variables.js 或 test.js 与您测试配置。

    test.js 需要 ENV=test 才能工作,而自定义环境变量需要类似(对于 Mac 和 NPM)$ npm run funcTest -> yarn serverRunning && NODE_ENV=test wdio wdio.conf.js。

    JSON 看起来像

    {
      test: 'Value'
    }
    

    【讨论】:

    • 感谢@MrPickles 的回复 - 我实际上已经在使用测试配置,但我需要修改它的值中间测试。我会用一个例子来更新我的问题!
    • 好的。我想不出配置会在中间过程中改变的时候,或者这是否可能。我猜你已经找到了一种方法,因此想要测试它。如果是这样......(暂停)......它可能需要一个新的测试(npm run test-config)才能获得预期的效果。但正如我所说,我想不出一种进入那种状态的方法,不幸的是我帮不上忙:(此时。但仍然会考虑一下。
    猜你喜欢
    • 1970-01-01
    • 2012-01-06
    • 2022-01-07
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多