테스트 시간에 일부 값을 무시하고 싶습니다. 특히 HTTP 서비스에 대한 재시도 횟수를 1 (즉시 실패, 재 시도 없음)로 설정하고 싶습니다. 우리 프로젝트는 node-config
을 사용합니다. docs에 따르면 나는 NODE_CONFIG
ENV 변수로 대체 할 수 있습니다 :런타임시 node-config로 구성 값을 대체하려면 어떻게합니까?
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`
어떻게이 재정의 작업합니까?
그것은이 앱 구성을 사용하여 config 폴더의 노드 설정에 development.json, production.json 등 test.json를 작성하는 것이 좋습니다
그게 효과가 있어요. 나는 지금 그것을보고 나서 왜 있는지 보았다. 귀하의 대답은 좀 더 많은 설명을 사용할 수 있습니다. – jcollum
답변이 더 자세하게 업데이트되었습니다. –