I have an app that depends on environmental variables like:
const APP_PORT = process.env.APP_PORT || 8080;
and I would like to test that fo
Jest's setupFiles is the proper way to handle this, and you need not install dotenv, nor use an .env file at all, to make it work.
dotenv
.env
jest.config.js:
jest.config.js
module.exports = { setupFiles: ["/.jest/setEnvVars.js"] };
.jest/setEnvVars.js:
.jest/setEnvVars.js
process.env.MY_CUSTOM_TEST_ENV_VAR = 'foo'
That's it.