AGENT SKILL

nestjs-dependency-injection

Use NestJS providers and injection tokens for testable, decoupled services

License 许可
Apache-2.0
所属仓库
linktogo/maggie
最近更新
2026-11-14
Stars
★ 2
Forks
2

01 前言 Front Matter

name
nestjs-dependency-injection
globs
[ "**/*.service.ts", "**/*.module.ts" ]
description
Use NestJS providers and injection tokens for testable, decoupled services

03 SKILL.md 正文

NestJS dependency injection

Let Nest wire dependencies. Never new a service that has its own dependencies — inject it so it can be swapped in tests.

Rules

  • Constructor injection with private readonly:
    constructor(private readonly users: UsersService) {}
    
  • Depend on an interface via an injection token when you need multiple implementations or want to fake I/O in tests:
    export const MAILER = Symbol('MAILER');
    // provider: { provide: MAILER, useClass: SmtpMailer }
    constructor(@Inject(MAILER) private readonly mailer: Mailer) {}
    
  • Default to the singleton scope. Reach for Scope.REQUEST only when a provider genuinely needs per-request state — it is slower and taints everything that injects it.
  • Configuration comes from ConfigService, not process.env read inline.

Testing

  • In unit tests, build a module with Test.createTestingModule and override providers (.overrideProvider(MAILER).useValue(fakeMailer)).
  • If a class is hard to test because of its dependencies, that is a design signal — inject a narrower abstraction rather than mocking the world.

04 同仓库更多 Skills