test process.env with Jest

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

    Expanding a bit on Serhan C.'s answer (https://stackoverflow.com/a/57944454/2708174)...

    according to this blog https://tekloon.dev/using-dotenv-with-jest you can include "dotenv/config" directly in setupFiles, without having to create and reference an external script that calls require("dotenv").config().

    i.e., simply do

    module.exports = {
        setupFiles: ["dotenv/config"]
    }
    

提交回复
热议问题