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
You can use setupFiles
feature of jest config. As documentation said that,
A list of paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment immediately before executing the test code itself.
npm install dotenv
dotenv that uses to access env variable..env
file to root directory of your application and add this line into it.#.env
APP_PORT=8080
//someModuleForTest.js
require("dotenv").config()
jest.config.js
file like thismodule.exports = {
setupFiles: ["./someModuleForTest"]
}
test("Some test name", () => {
expect(process.env.APP_PORT).toBe("8080")
})