
Dependency Injection with Claude Code: Testable TypeScript Without DI Containers
Dependency injection decouples modules and makes unit testing possible without spinning up a database. Claude Code designs complete DI patterns — interfaces, repositories, and mock implementations — when you define the rules. CLAUDE.md for Dependency Injection ## Dependency Injection Rules ### Core approach - Constructor injection as the default - DI containers: optional (small projects don't need them) - Services never instantiate dependencies internally ### Interface design - All external dependencies abstracted behind interfaces Examples: IEmailService, IUserRepository - Tests swap in mock implementations - Interfaces live in src/interfaces/ ### Naming conventions - Interfaces: I{Name} (IUserRepository) - Implementations: {Name} (UserRepository) - Test mocks: Mock{Name} (MockUserRepository) ### Testing - Unit tests don't use real DBs (mock the Repository) - DI-based code tests well with vitest + vi.fn() ### Prohibited - new keyword to instantiate dependencies inside service classes
Continue reading on Dev.to
Opens in a new tab

