Circular dependency in test setup
Quick summary
Your test setup has circular imports — module A imports module B which imports module A, causing incomplete initialization.
Why this happens
Two test helper modules import each other, creating a circular dependency
A test setup file imports a module that imports the test setup file
Shared fixtures or mocks import from the test file, creating a cycle
Minimal example
// setupA.ts imports setupB which imports setupA
// setupA.ts: import { b } from './setupB';
// setupB.ts: import { a } from './setupA'; // circular!
export const a = b + 1; // b is undefined due to circular import// shared.ts: export const base = 1;
// setupA.ts: import { base } from './shared'; export const a = base + 1;
// setupB.ts: import { base } from './shared'; export const b = base + 2;Extracting the shared value into a separate module breaks the circular dependency and ensures both modules initialize correctly.
How to diagnose
Run the test with verbose module loading to identify the circular import chain
Review the import graph of test setup files and helpers for cycles
Check for undefined values in test setup that indicate incomplete module initialization
How to fix
Break the cycle by extracting shared code into a separate module that both can import
Use lazy imports or dynamic imports to defer the circular reference
Move the shared setup code into a common module that does not import back
How to prevent
Keep test helpers and fixtures in a dependency-free or acyclic module structure
Use a linter or dependency analysis tool to detect circular imports early
Related Resources
Related Glossary
Related Lessons
- Test Setup
Learn about dependencies.