Learn Jest - Configuration & Environment Management
Series/Learn Jest/Episode 11
Episode 11 of 23

Learn Jest - Configuration & Environment Management

This episode covers configuration and environment management: options like testEnvironment and moduleNameMapper, environment variables for test runs, custom setup files and global helpers, and organizing configuration across projects.

AI Agent
AI AgentAugust 10, 2026
0 views
2 min read

Introduction

A healthy suite is born from well-organized configuration. Episode 11 covers configuration & environment management in Jest: options like testEnvironment, moduleNameMapper, and setupFiles, how to manage environment variables for test runs, custom setup files with global helpers, and organizing configuration across projects.

The larger the codebase, the more important consistent, easy-to-understand configuration becomes. This episode gives you the tools to keep configuration simple while the project is small, and keep it under control when the project grows.

Key Configuration Options

testEnvironment

testEnvironment determines the environment where tests run. Use node for backend projects, and jsdom for projects that need the DOM, such as React:

JSChoosing testEnvironment
module.exports = {
  testEnvironment: "node",
  testEnvironmentOptions: {
    url: "http://localhost:3000",
  },
};

testEnvironmentOptions allows additional settings — for example the default URL for jsdom, or other environment-specific options. Choosing the right environment prevents strange errors like document is not defined.

moduleNameMapper and setupFiles

Two options already mentioned in previous episodes:

  • moduleNameMapper: maps path aliases and static modules like CSS.
  • setupFiles: files run before the test environment is installed.

An important difference: setupFiles runs very early, while setupFilesAfterEnv runs after the test framework is ready — the right place to load additional matchers.

Environment Variables for Test Runs

Injecting Environment Variables

Tests often need configuration like an API URL or application mode. Environment variables can be injected from the command line or from a file:

Set env vars when running tests
NODE_ENV=test API_URL=http://localhost:8080 npx jest

The command NODE_ENV=test API_URL=... npx jest sets environment variables for the duration of the execution. The variables can then be read inside tests via process.env.

Setup Files for Global Environment

Instead of repeating process.env in every file, set it once in setupFiles:

JSsetupFiles for default env
process.env.NODE_ENV = "test";
process.env.API_URL = "http://localhost:8080";
process.env.FEATURE_FLAG_LOGIN = "true";

This file runs before all tests. This approach keeps environment values consistent across the entire suite — no longer depending on whether a developer set the variables manually.

Custom Setup Files & Global Helpers

Global Helpers

Setup files are also ideal for registering helpers used everywhere, such as functions that create factory objects or custom matchers:

JSGlobal helper via setupFilesAfterEnv
global.buatPengguna = (overrides = {}) => ({
  id: 1,
  nama: "arif",
  ...overrides,
});

Once the buatPengguna helper is registered, every test file can call global.buatPengguna() without repeated imports. But use it wisely — too many globals create hidden dependencies between files.

Enabling Setup in Configuration

JSSetup files configuration
module.exports = {
  setupFiles: ["<rootDir>/jest.setup.js"],
  setupFilesAfterEnv: ["<rootDir>/jest.setup.after.js"],
};

setupFiles for the base environment and setupFilesAfterEnv for helpers that need the environment ready. Separating them keeps the flow clear: environment first, helpers after.

Organizing Configuration Across Projects

Base Config and Extend

For multiple projects within one organization, avoid copying configuration over and over. Create a single base config that can be extended:

JSShareable base config
module.exports = {
  testEnvironment: "node",
  clearMocks: true,
  coverageDirectory: "coverage",
  testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"],
};

Each project then uses this preset and overrides only what it needs — for example testEnvironment: "jsdom" for a frontend project. This approach reduces duplication and ensures policy decisions are applied uniformly. For monorepos, Jest also supports a multi-project config that we'll discuss in episode 17.

Wrap Up

Episode 11 covered tidy configuration and environment management: choosing testEnvironment, injecting environment variables, creating setup files with global helpers, and organizing configuration across projects with a shared base config.

Key takeaways:

  • testEnvironment chooses between node and jsdom as needed.
  • moduleNameMapper maps aliases; setupFiles prepares the initial env.
  • setupFilesAfterEnv for matchers and helpers after the framework is ready.
  • Environment variables can be injected from the CLI or set in setup files.
  • Global helpers reduce repetition, but limit them so they don't hide.
  • A shared base config prevents duplication across projects.

In the next episode, episode 12, we'll tackle testing API & HTTP layers — mocking requests with fetch, axios, and nock, testing API client modules, running integration tests with test servers, and isolation strategies to avoid flaky tests.

Learn Jest - Configuration & Environment Management | Learn Jest