Skip to content

Jest Global Setup Not Running Fix

DodaTech Updated 2026-06-24 2 min read

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 globalSetup with globalTeardown
  • Use setupFiles for per-test-file configuration
  • Use setupFilesAfterEnv for framework extensions

Common Mistakes with config global setup

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to 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

### What's the difference between globalSetup and setupFiles?

globalSetup runs once before all test suites start. setupFiles runs before each test file. Use globalSetup for one-time infrastructure setup (start containers, seed databases) and setupFiles for per-test configuration (mock libraries, environment variables).

Can I access the globalSetup data in my tests?

Use the global object or environment variables. Set global.__MY_VAR__ in globalSetup and access it via global.__MY_VAR__ in tests. Environment variables set in globalSetup are also available.

Why does my globalSetup fail with "No tests found"?

Make sure your globalSetup script completes (resolves the promise) before Jest proceeds. An unhandled error or pending promise in globalSetup prevents test execution. Add error handling and logging to debug setup failures.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro