Jest Global Setup Not Running Fix
In this tutorial, you'll learn about Jest Global Setup Not Running Fix. We cover key concepts, practical examples, and best practices.
Your globalSetup script doesn't execute — the database isn't seeded, environment variables aren't set, and external services aren't started before tests run.
The Problem
// WRONG — globalSetup config with incorrect path
// jest.config.js
module.exports = {
globalSetup: './setup.js',
};
No error — but setup.js never runs
Jest expects globalSetup to be a module that exports a function. The path must be absolute or use <rootDir>.
Step-by-Step Fix
1. Create a proper globalSetup script
// RIGHT — globalSetup.js
module.exports = async () => {
process.env.DATABASE_URL = 'postgres://localhost:5432/test_db';
process.env.REDIS_URL = 'redis://localhost:6379/1';
// Start test containers
const { execSync } = require('child_process');
execSync('docker-compose -f docker-compose.test.yml up -d', {
stdio: 'inherit',
});
// Wait for services to be ready
await new Promise((resolve) => setTimeout(resolve, 5000));
};
// jest.config.js
module.exports = {
globalSetup: '<rootDir>/jest/globalSetup.js',
globalTeardown: '<rootDir>/jest/globalTeardown.js',
};
2. Create a matching globalTeardown
// RIGHT — globalTeardown.js
module.exports = async () => {
const { execSync } = require('child_process');
execSync('docker-compose -f docker-compose.test.yml down', {
stdio: 'inherit',
});
};
3. Pass data from setup to teardown
// RIGHT — share state via global object
// globalSetup.js
module.exports = async () => {
process.env.TEST_DB_PORT = '5433';
// Store data on global object for teardown
global.__TEST_CONTAINER_ID__ = 'my-container-id';
};
4. Use setupFiles for per-test-file setup
// RIGHT — setupFiles runs before each test file
// jest.config.js
module.exports = {
setupFiles: ['<rootDir>/jest/setupEnv.js'],
setupFilesAfterEnv: ['<rootDir>/jest/setupAfterEnv.js'],
globalSetup: '<rootDir>/jest/globalSetup.js',
};
Expected output:
$ npx jest
Global setup (4s)
PASS tests/integration.test.js
Global teardown (2s)
Prevention Tips
- Use absolute paths or
<rootDir>for script paths - Export an async function from setup scripts
- Always pair
globalSetupwithglobalTeardown - Use
setupFilesfor per-test-file configuration - Use
setupFilesAfterEnvfor framework extensions
Common Mistakes with config global setup
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world JEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro