Comments (5)
@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.
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.
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.
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.
@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)
- [BUG] Cannot find module 'ts-node' from 'parser.js' HOT 1
- Method Promise.prototype.then called on incompatible receiver #<Promise> HOT 1
- [BUG] TypeError: Cannot redefine property when sub module is using config.get() within the module HOT 7
- [BUG] Failing test on master HOT 5
- [BUG] JSON5 Vulnerability - Prototype Pollution in JSON5 via Parse Method HOT 2
- [BUG] config.get('variable') can not read from custom-environment-variables HOT 10
- [BUG] Comment stripping for JSON in node 19 doesn't work HOT 23
- [BUG] [Docs] Documentation is silent on the 'load order' of NODE_CONFIG variable HOT 2
- Allow different default.json for environments HOT 1
- [BUG] Improve the documentation on Warning "WARNING: NODE_ENV value of 'local' is ambiguous" HOT 2
- [BUG] RegExp are proxified which breaks them
- custom-environment-variables.json says variable is not defined. HOT 1
- Feature Request: First class Kubernetes secret files support HOT 3
- [BUG] Cypress component testing fails with error `<CONFIG_VARIABLE> is not defined`
- Deal With Async Properties in DeferConfig HOT 1
- [BUG] Documentation is missing HOT 4
- [BUG] custom-environment-variables file does not support cjs extension HOT 2
- [BUG] Extra quotes on config file broke app HOT 5
- Cannot use defer with esbuild (fix: export defer from config) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-config.