jco*_*lum 5 testing configuration node.js lab node-config
我想在测试时覆盖一些值,特别是将HTTP服务的重试次数设置为1(立即失败,不重试)。我们的项目使用node-config。根据文档,我可以用NODE_CONFIGenv变量覆盖:
node myapp.js --NODE_CONFIG='{"Customer":{"dbConfig":{"host":"customerdb.prod"}}}'
Run Code Online (Sandbox Code Playgroud)
好吧,我宁愿在我的测试中这样做,但不是对所有测试都这样做。该代码说,您可以通过设置允许配置突变ALLOW_CONFIG_MUTATIONS。
process.env.ALLOW_CONFIG_MUTATIONS = "true";
const importFresh = require('import-fresh');
importFresh("config");
process.env.NODE_CONFIG = JSON.stringify({httpServices:{integration:{enrich: {retryInterval: 1, retries: 1}}}});
expect(process.env.NODE_CONFIG, 'NODE_CONFIG not set').to.exist();
expect(process.env.NODE_CONFIG, 'NODE_CONFIG not set').to.match(/retryInterval/);
expect(process.env.ALLOW_CONFIG_MUTATIONS, 'ALLOW_CONFIG_MUTATIONS not set').to.equal("true");
const testConfig = require("config");
console.dir(testConfig.get("httpServices.integration.enrich"));
expect(testConfig.get("httpServices.integration.enrich.retryInterval"), 'config value not set to 1').to.equal(1);
Run Code Online (Sandbox Code Playgroud)
结果:
{ url: 'https://internal-**********',
retryInterval: 5000,
retries: 5 }
`Error: config value not set to 1: Expected 5000 to equal specified value: 1`
Run Code Online (Sandbox Code Playgroud)
我如何获得此替代才能正常工作?
(预期来自Hapi.js代码库)
我是的维护者之一node-config。您的错误是您require第二次使用时应该importFresh再次使用。
您首次使用“ importFresh()”并没有什么不同require(),因为它是的首次使用require()。
设置了一些变量后,调用require(),它将返回config已生成和缓存的副本,而忽略环境变量集的影响。
您只需importFresh()在当前使用的位置使用一次即可require()。如预期的那样,这将导致返回配置对象的“新”副本。
| 归档时间: |
|
| 查看次数: |
3240 次 |
| 最近记录: |