【发布时间】:2017-09-22 16:59:36
【问题描述】:
我想在测试时覆盖一些值,特别是将我的 http 服务重试次数设置为 1(立即失败,不重试)。我们的项目使用node-config。根据docs,我可以用NODE_CONFIG env 变量覆盖:
node myapp.js --NODE_CONFIG='{"Customer":{"dbConfig":{"host":"customerdb.prod"}}}'
好吧,我更愿意在我的测试中这样做,但不是针对所有测试。 code 表示您可以通过设置 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);
结果:
{ url: 'https://internal-**********',
retryInterval: 5000,
retries: 5 }
`Error: config value not set to 1: Expected 5000 to equal specified value: 1`
如何让这个覆盖生效?
(期望来自 Hapi.js 代码库)
【问题讨论】:
标签: node.js testing configuration lab node-config