test process.env with Jest

后端 未结 9 1969
南笙
南笙 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:18

    Another option is to add it to the jest.config.js file after the module.exports definition:

    process.env = Object.assign(process.env, {
      VAR_NAME: 'varValue',
      VAR_NAME_2: 'varValue2'
    });
    

    This way it's not necessary to define the ENV variables in each .spec file and they can be adjusted globally.

提交回复
热议问题