test process.env with Jest

后端 未结 9 1965
南笙
南笙 2020-11-28 04:00

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

9条回答
  •  青春惊慌失措
    2020-11-28 04:03

    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.

    jest.config.js:

    module.exports = {
      setupFiles: ["/.jest/setEnvVars.js"]
    };
    

    .jest/setEnvVars.js:

    process.env.MY_CUSTOM_TEST_ENV_VAR = 'foo'
    

    That's it.

提交回复
热议问题