I have the following code below which returns certain data depending on NODE_ENV:
config.js
export const Config = (() => {
let data;
switch (process.env.NODE_ENV) {
case 'development':
data = '123';
break;
case 'production':
data = '456'
break;
default:
break;
}
return {
data
};
})();
This works well in my component when I set NODE_ENV. However in my test, I keep getting undefined as a result.
config.test.js
describe('Config', () => {
test('returns correct data if NODE_ENV is development', () => {
process.env = { ...process.env, NODE_ENV: 'development' };
expect(Config.data).toBe('123'); // returns undefined, expected '123'
});
test('returns correct data if NODE_ENV is production', () => {
process.env = { ...process.env, NODE_ENV: 'production' };
expect(Config.data).toBe('456'); // returns undefined, expected '456'
});
});
Again, Config.data
works fine in my React component when I start it up, but I guess I need to somehow initialize this for it to work in my tests? Any advice would be appreciated!