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.

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.
testEnvironment determines the environment where tests run. Use node for backend projects, and jsdom for projects that need the DOM, such as React:
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.
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.
Tests often need configuration like an API URL or application mode. Environment variables can be injected from the command line or from a file:
NODE_ENV=test API_URL=http://localhost:8080 npx jestThe 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.
Instead of repeating process.env in every file, set it once in setupFiles:
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.
Setup files are also ideal for registering helpers used everywhere, such as functions that create factory objects or custom matchers:
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.
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.
For multiple projects within one organization, avoid copying configuration over and over. Create a single base config that can be extended:
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.
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.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.