Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): dependencies not resolving for transient lazy providers #13804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is this function here only for debugging purposes or is it actively used? If it is used why not provide a more meaningful message?

Thanks!

}
}

@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');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Would it make sense to use Promise.all() or Promise.allSettled() for awaiting these imports in parallel for a slight perf boost?

Thanks!

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