Test process.env with Jest

I have an application that depends on environmental variables like:

const APP_PORT = process.env.APP_PORT || 8080;

And I would like to test that for example:

  • APP_PORT can be set by a Node.js environment variable.
  • or that an Express.js application is running on the port set with process.env.APP_PORT

How can I achieve this with Jest? Can I set these process.env variables before each test or should I mock it somehow maybe?

14 Answers
14

Leave a Comment