Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  53] [ 2]  / answers: 1 / hits: 39525  / 7 Years ago, sun, december 31, 2017, 12:00:00

I have a file with object that gets populated with process.env properties:



env.js





console.log('LOADING env.js');

const {
PROXY_PREFIX = '/api/',
USE_PROXY = 'true',
APP_PORT = '8080',
API_URL = 'https://api.address.com/',
NODE_ENV = 'production',
} = process.env;

const ENV = {
PROXY_PREFIX,
USE_PROXY,
APP_PORT,
API_URL,
NODE_ENV,
};

module.exports.ENV = ENV;


Now I try to test this file with different process.env properties:



env.test.js





const envFilePath = '../../config/env';

describe('environmental variables', () => {
const OLD_ENV = process.env;

beforeEach(() => {
process.env = { ...OLD_ENV };
delete process.env.NODE_ENV;
});

afterEach(() => {
process.env = OLD_ENV;
});

test('have default values', () => {
const { ENV } = require(envFilePath);
expect(ENV).toMatchSnapshot();
});

test('are string values (to avoid casting errors)', () => {
const { ENV } = require(envFilePath);
Object.values(ENV).forEach(val => expect(typeof val).toEqual('string'));
});

test('will receive process.env variables', () => {
process.env.NODE_ENV = 'dev';
process.env.PROXY_PREFIX = '/new-prefix/';
process.env.API_URL = 'https://new-api.com/';
process.env.APP_PORT = '7080';
process.env.USE_PROXY = 'false';

const { ENV } = require(envFilePath);

expect(ENV.NODE_ENV).toEqual('dev');
expect(ENV.PROXY_PREFIX).toEqual('/new-prefix/');
expect(ENV.API_URL).toEqual('https://new-api.com/');
expect(ENV.APP_PORT).toEqual('7080');
expect(ENV.USE_PROXY).toEqual('false');
});
});


Unfortunately, even though I try to load the file in every test separately the file gets loaded only once, making the third test fail with:




Expected value to equal:
dev
Received:
production



P.S. It doesn't fail when I run the test alone.



I also know that env.js loads only once because console.log('LOADING env.js'); gets fired only once.



I tried to invalidate Nodes cache like:



  beforeEach(() => {
delete require.cache[require.resolve(envFilePath)];
process.env = { ...OLD_ENV };
delete process.env.NODE_ENV;
});


but require.cache is empty {} before each test so it seems that Jest is somehow responsible for importing the file.



I also tried to run yarn jest --no-cache but didn't help.



So what I want is to load env.js before each test so I can test how it behaves with different node environmental variables.



jest@^22.0.4


More From » node.js

 Answers
51

You can use jest.resetModules() in beforeEach method to reset the already required modules





beforeEach(() => {
jest.resetModules()
process.env = { ...OLD_ENV };
delete process.env.NODE_ENV;
});

[#55570] Monday, December 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kamronr

Total Points: 749
Total Questions: 110
Total Answers: 122

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
kamronr questions
Mon, Dec 21, 20, 00:00, 3 Years ago
Fri, Oct 16, 20, 00:00, 4 Years ago
Sat, Oct 3, 20, 00:00, 4 Years ago
Sun, Jul 28, 19, 00:00, 5 Years ago
;