Skip to content

Commit

Permalink
Merge pull request #13804 from patrickacioli/fix-lazy-transient-provider
Browse files Browse the repository at this point in the history
fix(core): dependencies not resolving for transient lazy providers
  • Loading branch information
kamilmysliwiec authored Nov 25, 2024
2 parents 67e30e2 + daf0f88 commit 3324703
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { TransientLazyModule } from '../src/transient.module';
import { LazyController } from '../src/lazy.controller';
import * as chai from 'chai';
import { expect } from 'chai';
import * as request from 'supertest';

describe('Lazy Transient providers', () => {
let app: INestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [LazyController],
}).compile();

app = module.createNestApplication();
await app.init();
});

it('should not recreate dependencies for default scope', async () => {
const resultOne = await request(app.getHttpServer()).get('/lazy/transient');

const resultTwo = await request(app.getHttpServer()).get('/lazy/transient');

expect(resultOne.text).to.be.equal('Hi! Counter is 1');
expect(resultOne.statusCode).to.be.equal(200);

expect(resultTwo.text).to.be.equal('Hi! Counter is 2');
expect(resultTwo.statusCode).to.be.equal(200);
});
});
6 changes: 6 additions & 0 deletions integration/lazy-modules/src/eager.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { GlobalService } from './global.module';

@Injectable()
export class EagerService {
private counter = 0;
constructor(public globalService: GlobalService) {}

sayHello() {
this.counter++;
return 'Hi! Counter is ' + this.counter;
}
}

@Module({
Expand Down
18 changes: 18 additions & 0 deletions integration/lazy-modules/src/lazy.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, Get } from '@nestjs/common';
import { LazyModuleLoader } from '@nestjs/core';

@Controller('lazy')
export class LazyController {
constructor(private lazyLoadModule: LazyModuleLoader) {}

@Get('transient')
async exec() {
const { TransientLazyModule } = await import('./transient.module');
const moduleRef = await this.lazyLoadModule.load(() => TransientLazyModule);

const { TransientService } = await import('./transient.service');
const _service = await moduleRef.resolve(TransientService);

return _service.eager();
}
}
11 changes: 11 additions & 0 deletions integration/lazy-modules/src/transient.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { GlobalService } from './global.module';
import { EagerService } from './eager.module';
import { TransientService } from './transient.service';

@Module({
imports: [],
providers: [TransientService, GlobalService, EagerService],
exports: [TransientService],
})
export class TransientLazyModule {}
11 changes: 11 additions & 0 deletions integration/lazy-modules/src/transient.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Injectable, Scope } from '@nestjs/common';
import { EagerService } from './eager.module';

@Injectable({ scope: Scope.TRANSIENT })
export class TransientService {
constructor(private eagerService: EagerService) {}

eager() {
return this.eagerService.sayHello();
}
}
9 changes: 9 additions & 0 deletions packages/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
InjectionToken,
NestModule,
Provider,
Scope,
Type,
ValueProvider,
} from '@nestjs/common/interfaces';
Expand Down Expand Up @@ -253,6 +254,10 @@ export class Module {
return this.addCustomProvider(provider, this._providers, enhancerSubtype);
}

if (this.isTransientProvider(provider) && this.getProviderByKey(provider)) {
return provider;
}

this._providers.set(
provider,
new InstanceWrapper({
Expand Down Expand Up @@ -292,6 +297,10 @@ export class Module {
);
}

private isTransientProvider(provider: Type<any>): boolean {
return getClassScope(provider) === Scope.TRANSIENT;
}

public addCustomProvider(
provider:
| ClassProvider
Expand Down

0 comments on commit 3324703

Please sign in to comment.