Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load test environment variables in Jest #99

Closed
eric-burel opened this issue Apr 15, 2021 · 0 comments · Fixed by #115
Closed

Load test environment variables in Jest #99

eric-burel opened this issue Apr 15, 2021 · 0 comments · Fixed by #115
Assignees
Labels

Comments

@eric-burel
Copy link
Collaborator

eric-burel commented Apr 15, 2021

Is your feature request related to a problem? Please describe.

Step 1: load environment variables from .env.test (done already)

See new documentation: https://nextjs.org/docs/basic-features/environment-variables#test-environment-variables

We need to use @next/env to load .env.test (and .env.test.local etc. like Next does as a default) in Jest.

Step 2: load derived variables from next-config.env (to do)

The next config might set additional variables. So, after the .env is loaded, we also should set process.env with the same variable we create in next-config.js.This is in particular useful for environment variables that are dynamic (derived from other variables, from the package.json etc.).

Sample code:

This is a sample code, not working as is but shows the idea

// jest-global-setup.js
const { extendEnv } = require("../nextConfig/extendEnv"); // the code that compute .env need to be exposed outside of next-config.js, so we can reuse it here
// https://nextjs.org/docs/basic-features/environment-variables#test-environment-variables
const { loadEnvConfig } = require("@next/env");
module.exports = async () => {
  console.info(`Jest setupTests script will:
  console.log("Loading environment variables in Jest");
  // @see https://github.com/vercel/next.js/issues/17903#issuecomment-708902413
  await loadEnvConfig(process.env.PWD);
  console.log("process.env", process.env);
  // Compute next.config env => it defines the constructed variables, so we need
  // to run it in Jest for the config to work correctly
  const envVariables = extendEnv({}).env;
  Object.entries(envVariables).forEach(([varName, varValue]) => {
    console.log(
      "Setting up envrionement variable",
      varName,
      "to value",
      varValue
    );
    process.env[varName] = varValue;
  });
};

Expected result

You should be able to display an env variable in a Jest test:

  • that comes from .env.test
  • and another variable that comes from nextConfig env
@eric-burel eric-burel changed the title Load test environment variable Load test environment variables in Jest Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants