-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13804 from patrickacioli/fix-lazy-transient-provider
fix(core): dependencies not resolving for transient lazy providers
- Loading branch information
Showing
6 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
integration/lazy-modules/e2e/lazy-import-transient-providers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters