When injecting a Scope.TRANSIENT provider (LoggerService) into two different consumers, the instance is unexpectedly shared, even though transient providers are expected to create new instances per consumer based on the docs.
Minimal Reproduction:
import { Inject, Injectable, Scope } from '@nestjs/common';
@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService {
public context?: string;
}
@Injectable()
export class SecondService {
constructor(
@Inject(LoggerService)
public readonly loggerService: LoggerService,
) {
this.loggerService.context = 'SecondService';
}
}
@Injectable()
export class FirstService {
constructor(
@Inject(SecondService)
public readonly secondService: SecondService,
@Inject(LoggerService)
public readonly loggerService: LoggerService,
) {
this.loggerService.context = 'FirstService';
console.log(
this.loggerService.context, // 'FirstService'
this.secondService.loggerService.context, // also 'FirstService'
);
}
}